From c5baaafae69394082817ede9e6eb3910c4601a72 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 30 Jan 2004 15:21:42 +0000 Subject: * Replaced the SDF parser by a substantially faster Bison/Flex parser (roughly 80x faster). The absolutely latest version of Bison (1.875c) is required for reentrant GLR support, as well as a recent version of Flex (say, 2.5.31). Note that most Unix distributions ship with the prehistoric Flex 2.5.4, which doesn't support reentrancy. --- src/libexpr/parser.y | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/libexpr/parser.y (limited to 'src/libexpr/parser.y') diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y new file mode 100644 index 000000000000..45e6a98e843a --- /dev/null +++ b/src/libexpr/parser.y @@ -0,0 +1,128 @@ +%glr-parser +%pure-parser +%locations +%error-verbose +%parse-param { yyscan_t scanner } +%parse-param { void * data } +%lex-param { yyscan_t scanner } + +%{ +#include +#include +#include +#include + +#include "parser-tab.h" +#include "lexer-tab.h" + +void setParseResult(void * data, ATerm t); +void parseError(void * data, char * error, int line, int column); +ATerm absParsedPath(void * data, ATerm t); + +void yyerror(YYLTYPE * loc, yyscan_t scanner, void * data, char * s) +{ + parseError(data, s, loc->first_line, loc->first_column); +} + +%} + +%union { + ATerm t; + ATermList ts; +} + +%type start expr expr_function expr_assert expr_op +%type expr_app expr_select expr_simple bind formal +%type binds expr_list formals +%token ID INT STR PATH URI +%token IF THEN ELSE ASSERT LET REC EQ NEQ AND OR IMPL + +%nonassoc IMPL +%left OR +%left AND +%nonassoc EQ NEQ +%left NEG + +%% + +start: expr { setParseResult(data, $1); }; + +expr: expr_function; + +expr_function + : '{' formals '}' ':' expr_function + { $$ = ATmake("Function(, )", $2, $5); } + | expr_assert + ; + +expr_assert + : ASSERT expr ';' expr_assert + { $$ = ATmake("Assert(, )", $2, $4); } + | expr_op + ; + +expr_op + : '!' expr_op %prec NEG { $$ = ATmake("OpNot()", $2); } + | expr_op EQ expr_op { $$ = ATmake("OpEq(, )", $1, $3); } + | expr_op NEQ expr_op { $$ = ATmake("OpNEq(, )", $1, $3); } + | expr_op AND expr_op { $$ = ATmake("OpAnd(, )", $1, $3); } + | expr_op OR expr_op { $$ = ATmake("OpOr(, )", $1, $3); } + | expr_op IMPL expr_op { $$ = ATmake("OpImpl(, )", $1, $3); } + | expr_app + ; + +expr_app + : expr_app expr_select + { $$ = ATmake("Call(, )", $1, $2); } + | expr_select { $$ = $1; } + ; + +expr_select + : expr_select '.' ID + { $$ = ATmake("Select(, )", $1, $3); } + | expr_simple { $$ = $1; } + ; + +expr_simple + : ID { $$ = ATmake("Var()", $1); } + | STR { $$ = ATmake("Str()", $1); } + | PATH { $$ = ATmake("Path()", absParsedPath(data, $1)); } + | URI { $$ = ATmake("Uri()", $1); } + | '(' expr ')' { $$ = $2; } + | LET '{' binds '}' { $$ = ATmake("LetRec()", $3); } + | REC '{' binds '}' { $$ = ATmake("Rec()", $3); } + | '{' binds '}' { $$ = ATmake("Attrs()", $2); } + | '[' expr_list ']' { $$ = ATmake("List()", $2); } + | IF expr THEN expr ELSE expr + { $$ = ATmake("If(, , )", $2, $4, $6); } + ; + +binds + : binds bind { $$ = ATinsert($1, $2); } + | { $$ = ATempty; } + ; + +bind + : ID '=' expr ';' + { $$ = ATmake("Bind(, )", $1, $3); } + ; + +expr_list + : expr_select expr_list { $$ = ATinsert($2, $1); } + /* yes, this is right-recursive, but it doesn't matter since + otherwise we would need ATreverse which requires unbounded + stack space */ + | { $$ = ATempty; } + ; + +formals + : formal ',' formals { $$ = ATinsert($3, $1); } /* idem - right recursive */ + | formal { $$ = ATinsert(ATempty, $1); } + ; + +formal + : ID { $$ = ATmake("NoDefFormal()", $1); } + | ID '?' expr { $$ = ATmake("DefFormal(, )", $1, $3); } + ; + +%% -- cgit 1.4.1