Commits

Corentin Derbois committed 6296478

Fix parser and lexer. Test "main.c" OK

Comments (0)

Files changed (2)

 {
    open Parser        (* The type token is defined in parser.mli *)
    exception Eof
-   let get = Lexing.lexeme
+   exception BadChar of char
+   let get = Lexing.lexeme   
+   
+   let incr_loc lexbuf delta =
+     let pos = lexbuf.Lexing.lex_curr_p in
+       lexbuf.Lexing.lex_curr_p <- { pos with
+         Lexing.pos_lnum = pos.Lexing.pos_lnum + 1;
+         Lexing.pos_bol = pos.Lexing.pos_cnum - delta;
+       }
+;;
 }
 
 let eol = '\013' '\010' | '\013' | '\010'
 let char  = '_' | ['a'-'z'] | ['A'-'Z'] | ['0'-'9']
 
+let D =   ['0'-'9']
+let L =   ['a'-'z' 'A'-'Z' '_']
+
 rule token = parse
- "auto"			{AUTO}
-|"break"		{BREAK}
-|"case"			{CASE}
-|"char"			{CHAR}
-|"const"		{CONST}
-|"continue"		{CONTINUE}
-|"default"		{DEFAULT}
-|"do"			{DO}
-|"double"		{DOUBLE}
-|"else"			{ELSE}
-|"enum"			{ENUM}
-|"extern"		{EXTERN}
-|"float"		{FLOAT}
-|"for"			{FOR}
-|"goto"			{GOTO}
-|"if"			{IF}
-|"int"			{INT}
-|"long"			{LONG}
-|"register"		{REGISTER}
-|"return"		{RETURN}
-|"short"		{SHORT}
-|"signed"		{SIGNED}
-|"sizeof"		{SIZEOF}
-|"static"		{STATIC}
-|"struct"		{STRUCT}
-|"switch"		{SWITCH}
-|"typedef"		{TYPEDEF}
-|"union"		{UNION}
-|"unsigned"		{UNSIGNED}
-|"void"			{VOID}
-|"volatile"		{VOLATILE}
-|"while"		{WHILE}
-|"..."			{ELLIPSIS}
-|">>="			{RIGHT_ASSIGN}
-|"<<="			{LEFT_ASSIGN}
-|"+="			{ADD_ASSIGN}
-|"-="			{SUB_ASSIGN}
-|"*="			{MUL_ASSIGN}
-|"/="			{DIV_ASSIGN}
-|"%="			{MOD_ASSIGN}
-|"&="			{AND_ASSIGN}
-|"^="			{XOR_ASSIGN}
-|"|="			{OR_ASSIGN}
-|">>"			{RIGHT_OP}
-|"<<"			{LEFT_OP}
-|"++"			{INC_OP}
-|"--"			{DEC_OP}
-|"->"			{PTR_OP}
-|"&&"			{AND_OP}
-|"||"			{OR_OP}
-|"<="			{LE_OP}
-|">="			{GE_OP}
-|"=="			{EQ_OP}
-|"!="			{NE_OP}
+[' ' '\t'] { token lexbuf }
+| ['\n']	 { incr_loc lexbuf 0; token lexbuf }
+| "auto"			{AUTO}
+| "break"		{BREAK}
+| "case"			{CASE}
+| "char"			{CHAR}
+| "const"		{CONST}
+| "continue"		{CONTINUE}
+| "default"		{DEFAULT}
+| "do"			{DO}
+| "double"		{DOUBLE}
+| "else"			{ELSE}
+| "enum"			{ENUM}
+| "extern"		{EXTERN}
+| "float"		{FLOAT}
+| "for"			{FOR}
+| "goto"			{GOTO}
+| "if"			{IF}
+| "int"			{INT}
+| "long"		{LONG}
+| "register"{REGISTER}
+| "return"	{RETURN}
+| "short"		{SHORT}
+| "signed"	{SIGNED}
+| "sizeof"	{SIZEOF}
+| "static"	{STATIC}
+| "struct"	{STRUCT}
+| "switch"	{SWITCH}
+| "typedef"	{TYPEDEF}
+| "union"		{UNION}
+| "unsigned"{UNSIGNED}
+| "void"		{VOID}
+| "volatile"{VOLATILE}
+| "while"		{WHILE}
+(* Not totaly sure part *)
+| L(L|D*)*   {IDENTIFIER}
+| D+        {CONSTANT}
+| L?['"']L*['"'] {STRING_LITERAL}
+
+(* end of Not totaly ... *)
+| "..."			{ELLIPSIS}
+| ">>="			{RIGHT_ASSIGN}
+| "<<="			{LEFT_ASSIGN}
+| "+="			{ADD_ASSIGN}
+| "-="			{SUB_ASSIGN}
+| "*="			{MUL_ASSIGN}
+| "/="			{DIV_ASSIGN}
+| "%="			{MOD_ASSIGN}
+| "&="			{AND_ASSIGN}
+| "^="			{XOR_ASSIGN}
+| "|="			{OR_ASSIGN}
+| ">>"			{RIGHT_OP}
+| "<<"			{LEFT_OP}
+| "++"			{INC_OP}
+| "--"			{DEC_OP}
+| "->"			{PTR_OP}
+| "&&"			{AND_OP}
+| "||"			{OR_OP}
+| "<="			{LE_OP}
+| ">="			{GE_OP}
+| "=="			{EQ_OP}
+| "!="			{NE_OP}
+| ";"			{POINTVIRGULE}
+| "{"			{LEFTACO}
+| "}"			{RIGHTACO}
+| ","			{VIRGULE}
+| "("			{LEFTPARA}
+| ")"			{RIGHTPARA}
+| eof       {EOF}
+| _ as e    { raise (BadChar(e)) }
 %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
 %token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
 %token XOR_ASSIGN OR_ASSIGN TYPE_NAME
+%token LEFTPARA RIGHTPARA VIRGULE LEFTACO RIGHTACO POINTVIRGULE EOF
 
 %token TYPEDEF EXTERN STATIC AUTO REGISTER
 %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
 	: IDENTIFIER { Ast.None }
 	| CONSTANT { Ast.None }
 	| STRING_LITERAL { Ast.None }
-	| '(' expression ')' { Ast.None }
+	| LEFTPARA expression RIGHTPARA { Ast.None }
 	;
 
 postfix_expression
 	: primary_expression { Ast.None }
 	| postfix_expression '[' expression ']' { Ast.None }
-	| postfix_expression '(' ')' { Ast.None }
-	| postfix_expression '(' argument_expression_list ')' { Ast.None }
+	| postfix_expression LEFTPARA RIGHTPARA { Ast.None }
+	| postfix_expression LEFTPARA argument_expression_list RIGHTPARA { Ast.None }
 	| postfix_expression '.' IDENTIFIER { Ast.None }
 	| postfix_expression PTR_OP IDENTIFIER { Ast.None }
 	| postfix_expression INC_OP { Ast.None }
 
 argument_expression_list
 	: assignment_expression { Ast.None }
-	| argument_expression_list ',' assignment_expression { Ast.None }
+	| argument_expression_list VIRGULE assignment_expression { Ast.None }
 	;
 
 unary_expression
 	| DEC_OP unary_expression { Ast.None }
 	| unary_operator cast_expression { Ast.None }
 	| SIZEOF unary_expression { Ast.None }
-	| SIZEOF '(' type_name ')' { Ast.None }
+	| SIZEOF LEFTPARA type_name RIGHTPARA { Ast.None }
 	;
 
 unary_operator
 
 cast_expression
 	: unary_expression { Ast.None }
-	| '(' type_name ')' cast_expression { Ast.None }
+	| LEFTPARA type_name RIGHTPARA cast_expression { Ast.None }
 	;
 
 multiplicative_expression
 
 expression
 	: assignment_expression { Ast.None }
-	| expression ',' assignment_expression { Ast.None }
+	| expression VIRGULE assignment_expression { Ast.None }
 	;
 
 constant_expression
 	;
 
 declaration
-	: declaration_specifiers ';' { Ast.None }
-	| declaration_specifiers init_declarator_list ';' { Ast.None }
+	: declaration_specifiers POINTVIRGULE { Ast.None }
+	| declaration_specifiers init_declarator_list POINTVIRGULE { Ast.None }
 	;
 
 declaration_specifiers
 
 init_declarator_list
 	: init_declarator { Ast.None }
-	| init_declarator_list ',' init_declarator { Ast.None }
+	| init_declarator_list VIRGULE init_declarator { Ast.None }
 	;
 
 init_declarator
 	;
 
 struct_or_union_specifier
-	: struct_or_union IDENTIFIER '{' struct_declaration_list '}' { Ast.None }
-	| struct_or_union '{' struct_declaration_list '}' { Ast.None }
+	: struct_or_union IDENTIFIER LEFTACO struct_declaration_list RIGHTACO { Ast.None }
+	| struct_or_union LEFTACO struct_declaration_list RIGHTACO { Ast.None }
 	| struct_or_union IDENTIFIER { Ast.None }
 	;
 
 	;
 
 struct_declaration
-	: specifier_qualifier_list struct_declarator_list ';' { Ast.None }
+	: specifier_qualifier_list struct_declarator_list POINTVIRGULE { Ast.None }
 	;
 
 specifier_qualifier_list
 
 struct_declarator_list
 	: struct_declarator { Ast.None }
-	| struct_declarator_list ',' struct_declarator { Ast.None }
+	| struct_declarator_list VIRGULE struct_declarator { Ast.None }
 	;
 
 struct_declarator
 	;
 
 enum_specifier
-	: ENUM '{' enumerator_list '}' { Ast.None }
-	| ENUM IDENTIFIER '{' enumerator_list '}' { Ast.None }
+	: ENUM LEFTACO enumerator_list RIGHTACO { Ast.None }
+	| ENUM IDENTIFIER LEFTACO enumerator_list RIGHTACO { Ast.None }
 	| ENUM IDENTIFIER { Ast.None }
 	;
 
 enumerator_list
 	: enumerator { Ast.None }
-	| enumerator_list ',' enumerator { Ast.None }
+	| enumerator_list VIRGULE enumerator { Ast.None }
 	;
 
 enumerator
 
 direct_declarator
 	: IDENTIFIER { Ast.None }
-	| '(' declarator ')' { Ast.None }
+	| LEFTPARA declarator RIGHTPARA { Ast.None }
 	| direct_declarator '[' constant_expression ']' { Ast.None }
 	| direct_declarator '[' ']' { Ast.None }
-	| direct_declarator '(' parameter_type_list ')' { Ast.None }
-	| direct_declarator '(' identifier_list ')' { Ast.None }
-	| direct_declarator '(' ')' { Ast.None }
+	| direct_declarator LEFTPARA parameter_type_list RIGHTPARA { Ast.None }
+	| direct_declarator LEFTPARA identifier_list RIGHTPARA { Ast.None }
+	| direct_declarator LEFTPARA RIGHTPARA { Ast.None }
 	;
 
 pointer
 
 parameter_type_list
 	: parameter_list { Ast.None }
-	| parameter_list ',' ELLIPSIS { Ast.None }
+	| parameter_list VIRGULE ELLIPSIS { Ast.None }
 	;
 
 parameter_list
 	: parameter_declaration { Ast.None }
-	| parameter_list ',' parameter_declaration { Ast.None }
+	| parameter_list VIRGULE parameter_declaration { Ast.None }
 	;
 
 parameter_declaration
 
 identifier_list
 	: IDENTIFIER { Ast.None }
-	| identifier_list ',' IDENTIFIER { Ast.None }
+	| identifier_list VIRGULE IDENTIFIER { Ast.None }
 	;
 
 type_name
 	;
 
 direct_abstract_declarator
-	: '(' abstract_declarator ')' { Ast.None }
+	: LEFTPARA abstract_declarator RIGHTPARA { Ast.None }
 	| '[' ']' { Ast.None }
 	| '[' constant_expression ']' { Ast.None }
 	| direct_abstract_declarator '[' ']' { Ast.None }
 	| direct_abstract_declarator '[' constant_expression ']' { Ast.None }
-	| '(' ')' { Ast.None }
-	| '(' parameter_type_list ')' { Ast.None }
-	| direct_abstract_declarator '(' ')' { Ast.None }
-	| direct_abstract_declarator '(' parameter_type_list ')' { Ast.None }
+	| LEFTPARA RIGHTPARA { Ast.None }
+	| LEFTPARA parameter_type_list RIGHTPARA { Ast.None }
+	| direct_abstract_declarator LEFTPARA RIGHTPARA { Ast.None }
+	| direct_abstract_declarator LEFTPARA parameter_type_list RIGHTPARA { Ast.None }
 	;
 
 cinitializer
 	: assignment_expression { Ast.None }
-	| '{' initializer_list '}' { Ast.None }
-	| '{' initializer_list ',' '}' { Ast.None }
+	| LEFTACO initializer_list RIGHTACO { Ast.None }
+	| LEFTACO initializer_list VIRGULE RIGHTACO { Ast.None }
 	;
 
 initializer_list
 	: cinitializer{ Ast.None }
-	| initializer_list ',' cinitializer{ Ast.None }
+	| initializer_list VIRGULE cinitializer{ Ast.None }
 	;
 
 statement
 	;
 
 compound_statement
-	: '{' '}' { Ast.None }
-	| '{' statement_list '}' { Ast.None }
-	| '{' declaration_list '}' { Ast.None }
-	| '{' declaration_list statement_list '}' { Ast.None }
+	: LEFTACO RIGHTACO { Ast.None }
+	| LEFTACO statement_list RIGHTACO { Ast.None }
+	| LEFTACO declaration_list RIGHTACO { Ast.None }
+	| LEFTACO declaration_list statement_list RIGHTACO { Ast.None }
 	;
 
 declaration_list
 	;
 
 expression_statement
-	: ';' { Ast.None }
-	| expression ';' { Ast.None }
+	: POINTVIRGULE { Ast.None }
+	| expression POINTVIRGULE { Ast.None }
 	;
 
 selection_statement
-	: IF '(' expression ')' statement { Ast.None }
-	| IF '(' expression ')' statement ELSE statement { Ast.None }
-	| SWITCH '(' expression ')' statement { Ast.None }
+	: IF LEFTPARA expression RIGHTPARA statement { Ast.None }
+	| IF LEFTPARA expression RIGHTPARA statement ELSE statement { Ast.None }
+	| SWITCH LEFTPARA expression RIGHTPARA statement { Ast.None }
 	;
 
 iteration_statement
-	: WHILE '(' expression ')' statement { Ast.None }
-	| DO statement WHILE '(' expression ')' ';' { Ast.None }
-	| FOR '(' expression_statement expression_statement ')' statement { Ast.None }
-	| FOR '(' expression_statement expression_statement expression ')' statement { Ast.None }
+	: WHILE LEFTPARA expression RIGHTPARA statement { Ast.None }
+	| DO statement WHILE LEFTPARA expression RIGHTPARA POINTVIRGULE { Ast.None }
+	| FOR LEFTPARA expression_statement expression_statement RIGHTPARA statement { Ast.None }
+	| FOR LEFTPARA expression_statement expression_statement expression RIGHTPARA statement { Ast.None }
 	;
 
 jump_statement
-	: GOTO IDENTIFIER ';' { Ast.None }
-	| CONTINUE ';' { Ast.None }
-	| BREAK ';' { Ast.None }
-	| RETURN ';' { Ast.None }
-	| RETURN expression ';' { Ast.None }
+	: GOTO IDENTIFIER POINTVIRGULE { Ast.None }
+	| CONTINUE POINTVIRGULE { Ast.None }
+	| BREAK POINTVIRGULE { Ast.None }
+	| RETURN POINTVIRGULE { Ast.None }
+	| RETURN expression POINTVIRGULE { Ast.None }
 	;
 
 translation_unit
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.