bison

Parsing optional semicolon at statement end

回眸只為那壹抹淺笑 提交于 2019-12-21 08:02:09
问题 I was writing a parser to parse C-like grammars. First, it could now parse code like: a = 1; b = 2; Now I want to make the semicolon at the end of line optional. The original YACC rule was: stmt: expr ';' { ... } Where the new line is processed by the lexer that written by myself(the code are simplified): rule(/\r\n|\r|\n/) { increase_lineno(); return :PASS } the instruction :PASS here is equivalent to return nothing in LEX, which drop current matched text and skip to the next rule, just like

Using precedence in Bison for unary minus doesn't solve shift/reduce conflict

断了今生、忘了曾经 提交于 2019-12-21 06:00:45
问题 I'm devising a very simple grammar, where I use the unary minus operand. However, I get a shift/reduce conflict. In the Bison manual, and everywhere else I look, it says that I should define a new token and give it higher precedence than the binary minus operand, and then use "%prec TOKEN" in the rule. I've done that, but I still get the warning. Why? I'm using bison (GNU Bison) 2.4.1. The grammar is shown below: %{ #include <string> extern "C" int yylex(void); %} %union { std::string token;

How to use yylval with strings in yacc

天大地大妈咪最大 提交于 2019-12-21 05:06:10
问题 I want to pass the actual string of a token. If I have a token called ID, then I want my yacc file to actually know what ID is called. I thing I have to pass a string using yylval to the yacc file from the flex file. How do I do that? 回答1: See the Flex manual section on Interfacing with YACC. 15 Interfacing with Yacc One of the main uses of flex is as a companion to the yacc parser-generator. yacc parsers expect to call a routine named yylex() to find the next input token. The routine is

How to use yylval with strings in yacc

折月煮酒 提交于 2019-12-21 05:06:01
问题 I want to pass the actual string of a token. If I have a token called ID, then I want my yacc file to actually know what ID is called. I thing I have to pass a string using yylval to the yacc file from the flex file. How do I do that? 回答1: See the Flex manual section on Interfacing with YACC. 15 Interfacing with Yacc One of the main uses of flex is as a companion to the yacc parser-generator. yacc parsers expect to call a routine named yylex() to find the next input token. The routine is

Bison - how to print a parse tree

两盒软妹~` 提交于 2019-12-21 03:15:31
问题 Hi I'm working on a small bison to learn how it works. The bison is supposed to parse a sentence. The sentence is made of expressions and expressions are made of words. Following is my code: %{ #include <stdio.h> #include <string.h> void yyerror(const char *str) { fprintf(stderr,"error: %s\n",str); } int yywrap() { return 1; } main() { yyparse(); } %} %token ASSIGN RANGE OR AND WHITESPACE QUOTE LPAREN RPAREN NOT GREATER LESS %union { int number; char *string; } %token <number> VALUE %token

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&bison翻译】写在前面

谁说我不能喝 提交于 2019-12-20 00:54:05
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 从此文开始,我将连载翻译O'Reilly出版的,由John Levine著的《flex & bison》一书(见下图)。 一来强化自己的知识水平,二来能让自己静下心来细细读一本英文著作。毕竟自己读是一种感受,翻译出来,字斟句酌,又是另外一种感受。如果一不小心能给读者带来些许的好处,那我就更加开心了。 说起自己接触flex和bison的历史,要从2010年4月刚进现在这家公司说起了,当时做的一个项目中,我的任务是要将一块电路板上所有的 pin,port,chain,net等信息从指定的文件格式(如BSDL,HSDL,CLM等)中抓取出来,存到数据结构中,然后进行进一步处理。而抓 取信息的过程,就是将指定格式的文件进行词法语法的扫描解析,用到的工具就是今天的主角:flex和bison。在做该项目的时候对flex和bison 进行了粗略的学习,后来也没有太深入系统的学习过,业界使用该工具开发的应用程序也不多,可是我还是觉得flex和bison是很不错的两个工具,尤其是 在了解一些脚本程序(如Tcl,Perl,Ruby等)背后执行的原理时,经常要用到词法语法分析。因此拥有了这两项技能,能帮助我们理解很多更深层次的 东西。 废话不多说,下一篇正式开始flex和bison之旅。 来源: oschina 链接: https:

【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

如何用flex+bison写语法分析器

无人久伴 提交于 2019-12-19 23:27:53
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 背景 这个星期,项目中要使用C++或C语言解析JSON格式的数据,把解析的结果放到一个通用的数据结构。这个通用的数据结构,实际上是作为web服务层(这一层大家可以认为是类似于PHP服务器或webpy的服务器容器)到web页面层(这一层是语法类似PHP脚本或者tornardo模板)的数据传输的协议。 之所以要这样处理, 主要是因为这个web类的项目(一般的web类项目也是如此)需求变化较快,而web的服务层使用是采用C++进行开发的,为了使当web服务层的数据格式变化不影响web页面层,所以双方使用统一的通用的数据结构。而之所以交代这么多的背景是, 为了让大家了解为什么我们不使用类似rapidjson或jsoncpp来实现json的解析而需要手写解析器。 因为使用类似rapidJson或者是jsoncpp之类的Json解析器,相当于我们要做: JSON文档 -> json DOM -> 通用数据结构。 而如果手写解析器,只需要做: JSON文档 -> 通用数据结构。 少一层转换能换来很多效率的提升。 说了这么多,下面开始进入正题。 以前学编译原理的时候,老师推荐过LEX /YACC来写编译器,其实这是古老的UNIX软件。 LINUX上有他们的GNU版本 FLEX、BISON。 这两个东西一个是词法分析器

Bison shift/reduce conflict / reduce/reduce conflict warnings

风格不统一 提交于 2019-12-19 04:55:08
问题 When i run this bison code in Ubuntu Linux i get these warnings : 1shift/reduce conflict [-Wconflicts-sr] 2 reduce/reduce conflicts [-Wcolficts-sr] Here's a screenshot for more clarity: http://i.imgur.com/iznzSsn.png Edit: reduce/reduce errors are in line 86 : typos_dedomenwn line 101: typos_synartisis and the shift/reduce error is in: line 129: entoli_if I can't find how to fix them could someone help? Here's the bison code bellow : %{ #include <stdio.h> #include <stdlib.h> #include <string