feat(parser/lexer): Complete parser/lexer

This commit is contained in:
Lucàs
2023-04-07 17:35:09 +02:00
parent 27c1366c11
commit e691dc72f8
5 changed files with 62 additions and 56 deletions
+1
View File
@@ -0,0 +1 @@
*.output
+1 -1
View File
@@ -26,7 +26,7 @@ lexer.ml: lexer.mll lang.cmo
ocamllex $<
parser.ml parser.mli: parser.mly lang.cmo
ocamlyacc $<
ocamlyacc -v $<
lexer.cmo: lexer.ml parser.cmo
ocamlc -c $<
+11 -3
View File
@@ -1,12 +1,20 @@
void main (string y, int x) {
void yes( int yes){
yes = 5;
return yes;
}
void main (string y, int x, int yooo) {
x = 15;
while (x < 20) {
x = x + 1;
x = x + 1 -16;
y = "hello";
if (x == 17) {
y = "world";
}
/* */
}
yooo = yes(5);
return x;
}
+7 -7
View File
@@ -39,7 +39,7 @@ rule token = parse
| "string" {TP(StringT)}
| "float" {TP(FloatT)}
| "void" {TP(VoidT)}
| "lit" {TP(LitT)} (* Potentiellement list *)
| "lit" {TP(LitT)} (* faut demander *)
| '(' { LPAREN }
| ')' { RPAREN }
@@ -73,16 +73,16 @@ rule token = parse
| "while" {WHILE}
| "for" {FOR}
| "true" {BCONSTANT(true)}
| "false" {BCONSTANT(false)}
| num_virgule as s {FLOATCONSTANT(float_of_string s)}
| num as s {INTCONSTANT(int_of_string s)}
| literal as l { LITCONSTANT l }
| str as s { STRINGCONSTANT s }
| "True" {BCONSTANT(true)}
| "False" {BCONSTANT(false)}
| eof {EOF}
| num_virgule as s {FLOATCONSTANT(float_of_string s)}
| num as s {INTCONSTANT(int_of_string s)}
| literal as l { LITCONSTANT l }
| str as s { STRINGCONSTANT s }
| alph alph* as i {IDENTIFIER i}
| _ {Printf.printf "ERROR: unrecogized symbol '%s'\n" (Lexing.lexeme lexbuf);
+42 -45
View File
@@ -15,26 +15,33 @@ open Lang
%token IF ELSE WHILE FOR RETURN BCEQ BCGE BCGT BCLE BCLT BCNE BLAND BLOR
%token EOF
%left QMARK COLON
%left BLOR
%left BLAND
%left BCEQ BCNE
%left BCGE BCGT BCLE BCLT
%left PLUS MINUS FPLUS FMINUS
%left TIMES DIV FDIV FTIMES MOD
%left LPAREN RPAREN LBRACE RBRACE
%right IF ELSE
%right WHILE FOR
%right RETURN
%left RPAREN
%right LPAREN
%left SEMICOLON
%start start
%type <Lang.prog> start
%%
start: fundefn { Prog ([], [$1]) }
start: list_fundefn { Prog ([], $1) }
;
list_fundefn:
| {[]}
| fundefn list_fundefn {$1 :: $2}
;
fundefn: /* Compound-statement dans la doc --> 6.8.2 */
/* d'apres la doc, on peut lui mettre --> LBRACE RBRACE {Skip} */
|fundecl LBRACE block_item_list_opt RBRACE { Fundefn($1, $3) }
@@ -61,46 +68,44 @@ constant:
|FLOATCONSTANT {FloatV($1) }
;
/*expression_opt:
|{Skip}
|expression {$1}
;
*/
expression: /* A.2.1 ---> 6.5.1 */
|constant {Const($1)}
|IDENTIFIER {VarE($1)}
|constant {Const($1)}
|LPAREN expression RPAREN {$2}
|multiplicative_expression {$1}
|additive_expression {$1}
|relational_expression {$1}
|equality_expression {$1}
|logical_and_expression {$1}
|logical_or_expression {$1}
|conditional_expression {$1}
|IDENTIFIER LPAREN expr_list RPAREN {CallE($1,$3)}
;
/*les calculs*/
multiplicative_expression: /* 6.5.5 */
|expression TIMES expression {BinOp(BArith(BAmul), $1, $3)}
|expression FTIMES expression {BinOp(BArith(BAfmul), $1, $3)}
|expression DIV expression {BinOp(BArith(BAdiv), $1, $3)}
|expression TIMES expression {BinOp(BArith(BAmul), $1, $3)}
|expression FDIV expression {BinOp(BArith(BAfdiv), $1, $3)}
|expression DIV expression {BinOp(BArith(BAdiv), $1, $3)}
|expression MOD expression {BinOp(BArith(BAmod), $1, $3)}
;
additive_expression: /* 6.5.6 */
|expression PLUS expression {BinOp(BArith(BAadd), $1, $3)}
|expression MINUS expression {BinOp(BArith(BAsub), $1, $3)}
|expression FPLUS expression {BinOp(BArith(BAfadd), $1, $3)}
|expression FMINUS expression {BinOp(BArith(BAfsub), $1, $3)}
|expression PLUS expression {BinOp(BArith(BAadd), $1, $3)}
|expression MINUS expression {BinOp(BArith(BAsub), $1, $3)}
;
/*les comparaisons*/
relational_expression: /* 6.5.8 */
|expression BCLT expression {BinOp(BCompar(BClt), $1, $3)}
|expression BCGT expression {BinOp(BCompar(BCgt), $1, $3)}
|expression BCLE expression {BinOp(BCompar(BCle), $1, $3)}
|expression BCGE expression {BinOp(BCompar(BCge), $1, $3)}
|expression BCGT expression {BinOp(BCompar(BCgt), $1, $3)}
|expression BCLT expression {BinOp(BCompar(BClt), $1, $3)}
;
equality_expression: /* 6.5.9 */
|expression BCEQ expression {BinOp(BCompar(BCeq), $1, $3)}
@@ -110,6 +115,9 @@ equality_expression: /* 6.5.9 */
/*les operateurs booleens */
logical_and_expression:
|expression BLAND expression {BinOp(BBool(BBand), $1, $3)} /* 6.5.13 */
;
logical_or_expression:
|expression BLOR expression {BinOp(BBool(BBor), $1, $3)} /* 6.5.14 */
;
@@ -124,28 +132,32 @@ conditional_expression: /* 6.5.16 */
/*///////////// DEBUT DES STATEMENTS //////////////////*/
statement: /* A.2.3 ----> 6.8 */
|LBRACE block_item_list_opt RBRACE {$2} /* peut etre fundefn plutot vu que c'est le vrai compound_statement*/
|compound_statement {$1} /* peut etre fundefn plutot vu que c'est le vrai compound_statement*/
// |expression_statement {$1}
|select_statement {$1} /*if et else*/
|iteration_statement {$1} /*while et for*/
|jump_statement SEMICOLON {$1} /*return et break*/
|assignation SEMICOLON {$1}
|IDENTIFIER LPAREN expr_list RPAREN {CallC($1,$3)}
// Faut faire l'appel à des foncttions avec CondC( fname * (expr_list ) )
;
expr_list:
| { [] }
| expression expr_list{[$1] @ $2}
;
compound_statement: /* 6.8.2 */
|LBRACE block_item_list_opt RBRACE {$2}
;
block_item_list_opt: /* 6.8.2 */
|statement {$1}
|block_item_list_opt statement {Seq($1,$2)} /* pas sur du tout */
| {Skip}
|statement block_item_list_opt {Seq($1,$2)} /* pas sur du tout */
|{Skip}
;
block_item: /* 6.8.2 */
/* declaration de variable / function (A.2.2 dans la doc) */
|statement {$1}
;
select_statement: /* 6.8.4 */
|IF LPAREN expression RPAREN statement {CondC ($3, $5, Skip) }
|IF LPAREN expression RPAREN statement ELSE statement {CondC ($3, $5, $7) }
@@ -153,7 +165,7 @@ select_statement: /* 6.8.4 */
iteration_statement: /* 6.8.5 */
|WHILE LPAREN expression RPAREN statement {Loop (Seq (CondC ( $3, Skip, Exit), $5))}
/*|FOR LPAREN expression_opt SEMICOLON expression_opt SEMICOLON expression_opt RPAREN statement {Loop (Seq (CondC ( $5, Skip, Exit), $9))} */ /*Absolument pas sur*/
|FOR LPAREN expression SEMICOLON expression SEMICOLON expression RPAREN statement {Loop (Seq (CondC ( $5, Skip, Exit), $9))} /*Absolument pas sur*/
;
jump_statement: /* 6.8.6 */
@@ -182,22 +194,7 @@ assignation: /* 6.5.17 */ /* dans la doc ça fait parti des expressions mais pg
Demander au prof :
---Comment on différencie "==" et "="
---Type LitT (peut etre list ?)
--- Je vois pas comment déclarer une variable (utiliser Vardecl qui demande un tp * vname)
qui renvoie un type vardecl avec Assign qui demande vname * expr ( et qui renvoie un com)
pour pouvoir prendre en compte "int x = 15;" par exemple
---Comment ça marche pour appeler une fonction ou expressionn CallE et CallC ?
---|block_item_list_opt {$1} // peut etre fundefn plutot vu que c'est le vrai compound_statement
: d'apres la doc, fundefn c'est le compound_statement, mais il a pas le optionnel
associativité des opérateurs ? (dans le pdf TP2 : introduction à YACC, partie 4 --> développons la grammaire ---> tirer 2)
associativité des opérateurs ? (dans le pdf TP2 : introduction à YACC, partie 4 --> développons la grammaire ---> tirer 2)
*/