Add lexer tokens for integer, float, string constants and identifiers

This commit is contained in:
Josh Holtrop 2018-04-07 08:27:35 -04:00
parent 8795d30953
commit 47bb3f5219
2 changed files with 38 additions and 0 deletions

View File

@ -83,4 +83,35 @@ continue return TOK_CONTINUE;
sizeof return TOK_SIZEOF;
'[^\\]' return TOK_CHAR_CONST;
'\\.' return TOK_CHAR_CONST;
'\\n' (void)'\n'; return TOK_CHAR_CONST;
'\\t' (void)'\t'; return TOK_CHAR_CONST;
'\\r' (void)'\r'; return TOK_CHAR_CONST;
'\\b' (void)'\b'; return TOK_CHAR_CONST;
'\\f' (void)'\f'; return TOK_CHAR_CONST;
[0-9]+([uU][lL]?[lL]?)? return TOK_INT_CONST;
0[xX][0-9a-fA-F]+([uU][lL]?[lL]?)? return TOK_INT_CONST;
[0-9]*\.[0-9]*[fF]? return TOK_FLOAT_CONST;
\" BEGIN(str);
<str>{
\" return TOK_STR_CONST;
\\x[0-9A-Fa-f]{2} {
/* hexadecimal escape code */
unsigned int val;
(void) sscanf(yytext + 2, "%x", &val);
}
\\n (void)'\n';
\\t (void)'\t';
\\r (void)'\r';
\\b (void)'\b';
\\f (void)'\f';
\\. (void)yytext[1];
[^\\\"]+ (void)yytext;
}
[a-zA-Z_][a-zA-Z_0-9]* return TOK_IDENTIFIER;
%%

View File

@ -86,6 +86,13 @@ static void handle_error(const char * str, const YYLTYPE * yylloc);
%token TOK_SIZEOF;
%token TOK_CHAR_CONST;
%token TOK_INT_CONST;
%token TOK_FLOAT_CONST;
%token TOK_STR_CONST;
%token TOK_IDENTIFIER;
%%
translation_unit: TOK_PLUS;