yacc

Ignoring errors in yacc/lex

半世苍凉 提交于 2019-12-20 06:32:50
问题 I'm new to yacc/lex and I'm working on a parser that was written by someone else. I notice that when an undefined token is found, the parser returns an error and stops. Is there a simple way to just make it ignore completely lines that it cannot parse and just move on to the next one? 回答1: just add a rule that looks like . { // do nothing } at the bottom of all of your rules, and it will just ignore everything it comes across that doesn't fit any of the previous rules. Edit: if you have

Lex regex gets some extra characters

為{幸葍}努か 提交于 2019-12-20 06:01:07
问题 I have the following definition in my lex file: L [a-zA-Z_] A [a-zA-Z_0-9] %% {L}{A}* { yylval.id = yytext; return IDENTIFIER; } And I do the following in my YACC file: primary_expression : IDENTIFIER { puts("IDENTIFIER: "); printf("%s", $1); } My source code (the one I'm analyzing) has the following assignment: ab= 10; For some reason, that printf("%s", $1); part is printing ab= and not only ab . I'm pretty sure that's the section that is printing ab= because when I delete the printf("%s",

Flex/Lex - How to know if a variable was declared

依然范特西╮ 提交于 2019-12-20 01:38:50
问题 My grammar allows: C → id := E // assign a value/expression to a variable (VAR) C → print(id) // print variables(VAR) values To get it done, my lex file is: [a-z]{ yylval.var_index=get_var_index(yytext); return VAR; } get_var_index returns the index of the variable in the list, if it does not exist then it creates one. It is working! The problem is: Everytime a variable is matched on lex file it creates a index to that variable. I have to report if 'print(a)' is called and 'a' was not

YACC (Yet Another Compiler Compiler)

走远了吗. 提交于 2019-12-20 00:38:15
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> yacc(Yet Another Compiler Compiler) 是Unix/Linux上一个用来生成 编译器 的编译器(编译器代码生成器)。yacc生成的编译器主要是用C语言写成的语法解析器(Parser),需要与词法解析器Lex一起使用,再把两部份产生出来的C程序一并编译。yacc本来只在Unix系统上才有,但现时已普遍移植往Windows及其他平台。 分析程序生成器(parser generator)是一个指定某个格式中的一种语言的语法作为它的输入,并为该种语言产生分析过程以作为它的输出的程序。在历史上,分析程序生成器被称作编译-编译程序( compiler- compiler ),这是由于按照规律可将所有的编译步骤作为包含在分析程序中的动作来执行。现在的观点是将分析程序仅考虑为编译处理的一个部分,所以这个术语也就有些过时了。合并 LALR(1) 分析算法是一种常用的分析生成器,它被称作 Yacc( yet another compiler- compiler )。给出 Yacc 的概貌来,将使用Yacc为 TINY 语言开发一个分析程序。   作为 Yacc 对说明文件中的 %token NUMBER 声明的对应。Yacc 坚持定义所有的符号记号本身,而不是从别的地方引入一个定义

【flex&bison翻译】前言

时间秒杀一切 提交于 2019-12-20 00:37:59
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> ****** 译者注:去年的时候曾经计划翻译本书,后来终于还是抵不过懒惰,给放下了,有句经典的话:现在的努力,是为了小时候吹过的牛逼。现在体会深刻啊。。。 本文是在Ubuntu 12.04.1系统下,使用LibreOffice Writer工具一个字一个字码上的,然后再手动调整字体和字号发表的,发布出去的那一刻,感觉真的很好,希望自己能坚持下来,写博客,写技术文章,翻译技术文章和书籍。+U ****** #### 当然,有语句不通顺的地方,或者描述不准确的地方,请不吝指出,我会尽快修正。 #### $$$$ 本书翻译时对一些术语采用了以下翻译:lexical词法,syntax句法,grammar语法。是否合适还有待商榷。$$$$ Flex和 bison 这两个工具是专为开发编译器( compilers) 和解释器( interpreters) 的开发人员而设计的。但是 flex 和 bison 的功能不仅仅如此,只要对程序的输入信息进行匹配查找,或者程序本身是 CLI 界面的,都可以使用 flex 和 bison 来进行开发。更进一步来讲,它们(指 flex 和 bison 这两个工具,下同。)还可以快速构建应用程序原型,并易于修改和维护,因此对于一些非编译器开发人员, flex 和 bison

yacc - Precedence of a rule with no operator?

纵然是瞬间 提交于 2019-12-19 11:25:46
问题 Thinking about parsing regular expressions using yacc (I'm actually using PLY), some of the rules would be like the following: expr : expr expr expr : expr '|' expr expr : expr '*' The problem is, the first rule(concatenation) must take precedence over the second rule, but not the third one. However, the concatenation rule has no operator in it. How can I specify the precedence correctly in this case? Thank you! EDIT: I modified the rules to avoid the issue, but I'm still curious what was the

verbose error with ocamlyacc

*爱你&永不变心* 提交于 2019-12-19 04:17:05
问题 In bison, it is sufficient to add %verbose-error to the file to make the parser errors more verbose. Is there any way to gain similar functionality with ocamlyacc? Here is the answer for a similar question, but I could not make anything out of it. This is how I call the lexer and parser functions: let rec foo () = try let line = input_line stdin in (try let _ = (Parser.latexstatement lexer_token_safe (Lexing.from_string line)) in print_string ("SUCCESS\n") with LexerException s -> print

Simple Flex/Bison C++

三世轮回 提交于 2019-12-18 11:50:49
问题 I already looked for my answer but I didn't get any quick response for a simple example. I want to compile a flex/bison scanner+parser using g++ just because I want to use C++ classes to create AST and similar things. Searching over internet I've found some exploits, all saying that the only needed thing is to declare some function prototypes using extern "C" in lex file. So my shady.y file is %{ #include <stdio.h> #include "opcodes.h" #include "utils.h" void yyerror(const char *s) { fprintf

Shift Reduce Conflict

99封情书 提交于 2019-12-18 09:38:48
问题 I'm having trouble fixing a shift reduce conflict in my grammar. I tried to add -v to read the output of the issue and it guides me towards State 0 and mentions that my INT and FLOAT is reduced to variable_definitions by rule 9. I cannot see the conflict and I'm having trouble finding a solution. %{ #include <stdio.h> #include <stdlib.h> %} %token INT FLOAT %token ADDOP MULOP INCOP %token WHILE IF ELSE RETURN %token NUM ID %token INCLUDE %token STREAMIN ENDL STREAMOUT %token CIN COUT %token

Good parser generator (think lex/yacc or antlr) for .NET? Build time only? [closed]

六月ゝ 毕业季﹏ 提交于 2019-12-17 23:12:39
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Is there a good parser generator (think lex/yacc or antlr) for .NET? Any that have a license that would not scare lawyers? Lot’s of LGPL but I am working on embedded components and some organizations are not comfortable with me taking an LGPL dependency. I've heard that Oslo may provide this functionality but I