yacc

Lex和Yacc

陌路散爱 提交于 2019-12-10 15:28:01
lex负责词法解析,而yacc负责语法解析,其实说白了就是lex负责根据指定的正则表达式,将输入的字符串匹配成一个一个的token,同时允许用户将当前匹配到的字符串进行处理,并且允许返回一个标识当前token的标识码。而yacc则负责进行语法解析,将一个个的token最终形成一个完整的语法。 lex和yacc类似的,分为三个部分 %{ 这里可以写任何的c代码 比如一些初始化的状态 %} 这里是一些的lex或者yacc的定义 比如lex里的%s yacc里的%type %token %left %right %union %% 这里可以写任何的lex或者yacc代码 %% 这里可以写任何c代码 这里需要注意的是,对于一个语法分析器来说,可以不使用lex而自己根据需要来完成,但是使用lex可以直接使用正则来进行匹配,使得整个过程更加简单。而lex与yacc之间的通信就是靠%union里定义的联合体来完成。其实%union里定义的联合体最终会被生成一个叫yylval的全局变量,这个全局变量可以在lex和yacc之间传递变量。 %type<xxx> 这里的xxx必须是%union里定义的一个成员变量,那么%type<xxx> 里定义的类型就会被存储这个成员变量里 %token<xxx> 与%type类似,只是它所代表的是token而已 $$代表当前的type所计算的最终结果,而

Bison+Flex segfault no backtrace

最后都变了- 提交于 2019-12-10 12:35:20
问题 I'm trying to debug code generated by Bison + Flex (what a joy!). It segfaults so badly that there isn't even stack information available to gdb . Is there any way to make this combination generate code that's more debuggable? Note that I'm trying to compile a reentrant lexer and parser (which is in itself a huge pain). Below is the program that tries to use the yyparse : int main(int argc, char** argv) { int res; if (argc == 2) { yyscan_t yyscanner; res = yylex_init(&yyscanner); if (res != 0

Error recovery in an LALR(1) grammar

一世执手 提交于 2019-12-10 11:23:41
问题 I'm using some parser and lexer generating tools (similar to Lex and Bison, but for C#) to generate programs that parse strings into abstract syntax trees that can later be evaluated. I wanted to do error recovery (i.e. report in the produced abstract sentence tree that there are missing tokens and such). I had two approaches in mind to structuring the generated grammars, and I was wondering which approach was better/more flexible/wouldn't have conflicts (the .y and .lex files are generated

How to convert MySQL yacc grammar to antlr LL(1)?

▼魔方 西西 提交于 2019-12-10 11:03:19
问题 I am constructing a MySQL grammar validator with ANTLR. I started with the sql_yacc.yy from the MySQL source code, but I have some difficulties converting the following grammar. I tried many times, but it doesn't work. Can anyone help me? expr : expr or expr | expr XOR expr | expr and expr | NOT_SYM expr | bool_pri IS TRUE_SYM | bool_pri IS not TRUE_SYM | bool_pri IS FALSE_SYM | bool_pri IS not FALSE_SYM | bool_pri IS UNKNOWN_SYM | bool_pri IS not UNKNOWN_SYM | bool_pri ; bool_pri : bool_pri

yacc shift-reduce for ambiguous lambda syntax

家住魔仙堡 提交于 2019-12-10 10:57:16
问题 I'm writing a grammar for a toy language in Yacc (the one packaged with Go) and I have an expected shift-reduce conflict due to the following pseudo-issue. I have to distilled the problem grammar down to the following. start: stmt_list expr: INT | IDENT | lambda | '(' expr ')' { $$ = $2 } lambda: '(' params ')' '{' stmt_list '}' params: expr | params ',' expr stmt: /* empty */ | expr stmt_list: stmt | stmt_list ';' stmt A lambda function looks something like this: map((v) { v * 2 },

“make: yacc: Command not found” after installing Bison

我是研究僧i 提交于 2019-12-09 14:43:47
问题 While running a makefile in gcc 4.1.2 (linux 5), I got the following error make: yacc: Command not found By googling, I came to know that this error can be rectified by installing Bison-GNU parser generator. But even after installing Bison, I get the same error. How can this error be solved? 回答1: From the looks of things, your makefile is expecting a yacc executable to be available and either it's not, or it's not on your path. Since bison is supposed to be compatible with yacc so the first

Indentation control while developing a small python like language

纵饮孤独 提交于 2019-12-09 06:52:41
问题 I'm developing a small python like language using flex, byacc (for lexical and parsing) and C++, but i have a few questions regarding scope control. just as python it uses white spaces (or tabs) for indentation, not only that but i want to implement index breaking like for instance if you type "break 2" inside a while loop that's inside another while loop it would not only break from the last one but from the first loop as well (hence the number 2 after break) and so on. example: while 1

How to fix YACC shift/reduce conflicts from post-increment operator?

霸气de小男生 提交于 2019-12-08 20:16:00
问题 I'm writing a grammar in YACC (actually Bison), and I'm having a shift/reduce problem. It results from including the postfix increment and decrement operators. Here is a trimmed down version of the grammar: %token NUMBER ID INC DEC %left '+' '-' %left '*' '/' %right PREINC %left POSTINC %% expr: NUMBER | ID | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | INC expr %prec PREINC | DEC expr %prec PREINC | expr INC %prec POSTINC | expr DEC %prec POSTINC | '(' expr ')' ; %% Bison

Using bison to parse list of elements

我只是一个虾纸丫 提交于 2019-12-08 13:25:03
问题 I'm writing a compiler for a shading engine and every worked fine until I reached the statements parsing part. I used an abstract syntax tree defined with classes to do all the work (to simplify typechecking and intermediate code generation).. so I have an ancestor class ASTNode and all descending classes like ASTFloat , ASTExpression , ASTIdentifier and so on.. In .y file I'm able to build up the AST in the common way: nexp: T_LPAR nexp T_RPAR { $$ = $2; } | nexp OP_PLUS nexp { $$ = new

Unintentional concatenation in Bison/Yacc grammar

人走茶凉 提交于 2019-12-07 17:50:20
问题 I am experimenting with lex and yacc and have run into a strange issue, but I think it would be best to show you my code before detailing the issue. This is my lexer: %{ #include <stdlib.h> #include <string.h> #include "y.tab.h" void yyerror(char *); %} %% [a-zA-Z]+ { yylval.strV = yytext; return ID; } [0-9]+ { yylval.intV = atoi(yytext); return INTEGER; } [\n] { return *yytext; } [ \t] ; . yyerror("invalid character"); %% int yywrap(void) { return 1; } This is my parser: %{ #include <stdio.h