From d1b14397ed811ce9e0bc2cc30b74b3fc41ce7193 Mon Sep 17 00:00:00 2001 From: Felix Raimundo Date: Fri, 18 Mar 2016 11:09:43 +0100 Subject: [PATCH] Adds the priorities to the parser and lexer. Change-Id: I28950e802ab223460b2d283f2d7f4908ff8d9760 --- poincare/src/expression_lexer.l | 10 +++++----- poincare/src/expression_parser.y | 23 +++++++++-------------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/poincare/src/expression_lexer.l b/poincare/src/expression_lexer.l index b1f2249b1..26f416b47 100644 --- a/poincare/src/expression_lexer.l +++ b/poincare/src/expression_lexer.l @@ -49,13 +49,13 @@ %% [0-9]+ { yylval->string = yytext; return(INTEGER); } -\/ { return(DIVIDE); } -\* { return(MULTIPLY); } -\^ { return(POW); } +[A-Za-z]+ { yylval->string = yytext; return(SYMBOL); } \+ { return(PLUS); } +\- { return(MINUS); } +\* { return(MULTIPLY); } +\/ { return(DIVIDE); } +\^ { return(POW); } \( { return(LEFT_PARENTHESIS); } \) { return(RIGHT_PARENTHESIS); } -[A-Za-z]+ { yylval->string = yytext; return(SYMBOL); } %% - diff --git a/poincare/src/expression_parser.y b/poincare/src/expression_parser.y index 757316843..78470dc45 100644 --- a/poincare/src/expression_parser.y +++ b/poincare/src/expression_parser.y @@ -19,14 +19,6 @@ void poincare_expression_yyerror(yyscan_t scanner, Expression ** expressionOutpu * instead we do provide regular memcpy. Let's instruct Bison to use it. */ #define YYCOPY(To, From, Count) memcpy(To, From, (Count)*sizeof(*(From))) -/* - #include - using namespace ui::Private; - #include "css_selector_parser.hpp" - #include "css_selector_lexer.hpp" - CSSSelector::Combinator awe_css_combinator_from_string(std::string string); - void awe_css_yyerror(yyscan_t scanner, CSSSelector ** selector, char const *msg); -*/ %} /* We want a reentrant parser. Otherwise Bison makes use of global variables, @@ -57,11 +49,11 @@ void poincare_expression_yyerror(yyscan_t scanner, Expression ** expressionOutpu %token INTEGER %token SYMBOL -/* The DIVIDE and POW tokens use no value */ -%token DIVIDE -%token MULTIPLY -%token POW %token PLUS +%token MINUS +%token MULTIPLY +%token DIVIDE +%token POW %token LEFT_PARENTHESIS %token RIGHT_PARENTHESIS @@ -76,12 +68,15 @@ Root: *expressionOutput = $1; } +/* Note that in bison, precedence of parsing depend on the order they are defined in here, the last + * one has the highest precedence. */ exp: INTEGER { $$ = new Integer($1); } | SYMBOL { $$ = new Symbol($1); } - | exp DIVIDE exp { $$ = new Fraction($1,$3); } - | exp MULTIPLY exp { $$ = new Product($1,$3); } | exp PLUS exp { $$ = new Addition($1,$3); } + | exp MINUS exp { $$ = new Substraction($1,$3); } + | exp MULTIPLY exp { $$ = new Product($1,$3); } + | exp DIVIDE exp { $$ = new Fraction($1,$3); } | exp POW exp { $$ = new Power($1,$3); } | LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = $2 } ;