yacc

Resolve conflict in bison grammar with space separated expression lists + if/then/else

对着背影说爱祢 提交于 2019-12-29 09:34:47
问题 I have the following yacc/bison/happy grammar: %token if TokenIf then TokenThen else TokenElse true TokenTrue false TokenFalse %left APP %right IF %% Hungry : NoHungry | Hungry NoHungry %prec APP | if Hungry then Hungry else Hungry %prec IF NoHungry : true | false bison -v tells me there are two conflicts in the following situation: State 12 2 Hungry: Hungry . NoHungry 3 | if Hungry then Hungry else Hungry . true shift, and go to state 2 false shift, and go to state 3 true [reduce using rule

Parsing an equation with sub-formulas in python

霸气de小男生 提交于 2019-12-25 10:22:40
问题 I'm trying to develop an equation parser using a compiler approach in Python. The main issue that I encounter is that it is more likely that I don't have all the variables and need therefore to look for sub-formulas. Let's show an example that is worth a thousand words ;) I have four variables whom I know the values: vx, vy, vz and c: list_know_var = ['vx', 'vy', 'vz', 'c'] and I want to compute the Mach number (M) defined as equation = 'M = V / c' I already know the c variable but I don't

Parsing an equation with sub-formulas in python

亡梦爱人 提交于 2019-12-25 10:21:33
问题 I'm trying to develop an equation parser using a compiler approach in Python. The main issue that I encounter is that it is more likely that I don't have all the variables and need therefore to look for sub-formulas. Let's show an example that is worth a thousand words ;) I have four variables whom I know the values: vx, vy, vz and c: list_know_var = ['vx', 'vy', 'vz', 'c'] and I want to compute the Mach number (M) defined as equation = 'M = V / c' I already know the c variable but I don't

Bison - recover from If Else error

大兔子大兔子 提交于 2019-12-25 02:56:10
问题 I'm trying to recover from an error in an If-Else statement. In my grammar an If is always followed by an Else. statement: OBRACES statements CBRACES | IF OPAR exp CPAR statement ELSE statement | IF OPAR exp CPAR statement error '\n' { yyerrok; yyclearin;} ; The error found is in the commented else in the last lines: public boolean Equal(Element other){ if (!this.Compare(aux01,Age)) ret_val = false ; //else //nt = 0 ; } error: syntax error, unexpected CBRACES, expecting ELSE -> } @ line 29 It

ambiguity in parsing comma as a operator using PLY python

六眼飞鱼酱① 提交于 2019-12-25 01:34:34
问题 I have following tokens and many more, but I want to keep my question short that's why not including the whole code. tokens = ( 'COMMA', 'OP', 'FUNC1', 'FUNC2' ) def t_OP(t): r'&|-|\||,' return t def t_FUNC1(t): r'FUNC1' return t def t_FUNC2(t): r'FUNC2' return t Other methods: def FUNC1(param): return {'a','b','c','d'} def FUNC2(param,expression_result): return {'a','b','c','d'} My grammar rules in YACC are and few more are there but listed important ones: 'expression : expression OP

How to get more parse error information from lex / yacc?

有些话、适合烂在心里 提交于 2019-12-24 22:32:10
问题 How to get more parse error information from lex / yacc? Currently in the lex file I am using: int yyerror(const char *msg) { fprintf(stderr, "Parse error: %s\n", msg); return 0; } But when I run my program yyerror outputs a blank message. I tried adding yylineno and yytext to yyerror but these cause compilation errors. I tried adding "%error-verbose" to the yacc file and "%option debug" to the lex file but these made no difference to the message. I did notice however that yyparse outputs a

What's wrong with my grammar

本秂侑毒 提交于 2019-12-24 08:13:13
问题 I try to input the following into my yacc parser: int main(void) { return; } It looks valid to me according to what's defined in the yacc file, but I get a "syntax error" message after the return. Why is that? The yacc file: /* C-Minus BNF Grammar */ %{ #include "parser.h" #include <string.h> %} %union { int intval; struct symtab *symp; } %token ELSE %token IF %token INT %token RETURN %token VOID %token WHILE %token <symp> ID %token <intval> NUM %token LTE %token GTE %token EQUAL %token

How to create a symbol table if given a grammar in a yacc file?

不羁的心 提交于 2019-12-24 07:59:04
问题 I'm given a yacc file and I'm supposed to create a symbol table. What do I do after I use yacc on the file? 回答1: Your question is very broad, and there exist books and online resources to answer it. It would be best for you to do some reading and then come back and ask more targeted questions. Check out the answers to this StackOverflow question for links to Yacc tutorials: https://stackoverflow.com/questions/641701/excellent-online-tutorial-for-lex-and-yacc 来源: https://stackoverflow.com

Parse a block where each line starts with a specific symbol

风流意气都作罢 提交于 2019-12-24 01:58:07
问题 I need to parse a block of code which looks like this: * Block | Line 1 | Line 2 | ... It is easy to do: block : head lines; head : '*' line; lines : lines '|' line | '|' line ; Now I wonder, how can I add nested blocks, e.g.: * Block | Line 1 | * Subblock | | Line 1.1 | | ... | Line 2 | ... Can this be expressed as a LALR grammar? I can, of course, parse the top-level blocks and than run my parser again to deal with each of these top-level blocks. However, I'm just learning this topic so it

Yacc 与 Lex 快速入门

情到浓时终转凉″ 提交于 2019-12-23 21:21:45
developerWorks 中国 > Linux > Yacc 与 Lex 快速入门 Lex 与 Yacc 介绍 文档选项 打印本页 将此页作为电子邮件发送 级别: 初级 Ashish Bansal ( mailto:abansal@ieee.org?subject=Yacc 与 Lex 快速入门&cc=abansal@ieee.org ), 软件工程师, Sapient 公司 2000 年 11 月 01 日 Lex 和 Yacc 是 UNIX 两个非常重要的、功能强大的工具。事实上,如果你熟练掌握 Lex 和 Yacc 的话,它们的强大功能使创建 FORTRAN 和 C 的编译器如同儿戏。Ashish Bansal 为您详细的讨论了编写自己的语言和编译器所用到的这两种工具,包括常规表达式、声明、匹配模式、变量、Yacc 语法和解析器代码。最后,他解释了怎样把 Lex 和 Yacc 结合起来。 Lex 代表 Lexical Analyzar。Yacc 代表 Yet Another Compiler Compiler。 让我们从 Lex 开始吧。 Lex Lex 是一种生成扫描器的工具。扫描器是一种识别文本中的词汇模式的程序。这些词汇模式(或者常规表达式)在一种特殊的句子结构中定义,这个我们一会儿就要讨论。 一种匹配的常规表达式可能会包含相关的动作。这一动作可能还包括返回一个标记