From 97fb20538cdac668dda0a0aa82884505bc0df61a Mon Sep 17 00:00:00 2001 From: Teddy Date: Sun, 23 Mar 2014 16:54:19 +0800 Subject: lexical analysis almost done --- cibic.y | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 cibic.y (limited to 'cibic.y') diff --git a/cibic.y b/cibic.y new file mode 100644 index 0000000..92f0cfe --- /dev/null +++ b/cibic.y @@ -0,0 +1,43 @@ +%{ +#include +%} +%token IDENTIFIER INT_CONST CHAR_CONST STR_CONST +%token KW_VOID KW_CHAR KW_INT KW_STRUCT KW_UNION KW_IF KW_ELSE KW_WHILE +%token KW_FOR KW_CONT KW_BREAK KW_RET KW_SIZEOF +%token OPT_OR OPT_AND OPT_EQ OPT_NE OPT_LE OPT_GE OPT_SHL OPT_SHR OPT_INC OPT_DEC OPT_PTR +%token ASS_MUL ASS_DIV ASS_MOD ASS_ADD ASS_SUB ASS_SHL ASS_SHR ASS_AND ASS_XOR ASS_OR +%token UNKNOWN +%union { + int intval; + char *strval; +} +%% +program + : body { + printf("\n")} +body + : IDENTIFIER +%% +int yywrap() { + return 1; +} + +int yyerror(char *s) { +} + +extern FILE *yyin; +int main() { + int ret; + //yyin = fopen("in", "r"); + while (ret = yylex()) + { + printf("%d\n", ret); + if (ret == IDENTIFIER) + printf("id: %s\n", yylval.strval); + else if (ret == INT_CONST) + printf("int: %d\n", yylval.intval); + else if (ret == STR_CONST) + printf("str: %s\n", yylval.strval); + } + return 0; +} -- cgit v1.2.3