yacc

Why is yylval null?

对着背影说爱祢 提交于 2019-12-07 17:03:56
问题 I'm trying to write my first parser with Flex & Bison. When parsing numbers, I'm trying to save their values into the yylval structure. The problem is, yylval is null when the lexer reaches a number, which causes a segmentation fault. (Related point of confusion: why is it that in most Flex examples (e.g. here), yylval is a structure, rather than a pointer to a structure? I couldn't get yylval to be recognized in test.l without %option bison-bridge , and that option made yylval a pointer.

how to use `yy_scan_string(const char *str)` (generated by lex yacc ) in a separated file

烂漫一生 提交于 2019-12-07 16:53:55
问题 I want to use YY_BUFFER_STATE yy_scan_string(const char *str) and other functions like yyparse() in my main.cpp , I did things: extern "C"{ extern YY_BUFFER_STATE yy_scan_string(const char *str); } But there is a error error: YY_BUFFER_STATE' does not name a type`, then I did: extern yy_buffer_state; typedef yy_buffer_state *YY_BUFFER_STATE; extern int yyparse(); extern YY_BUFFER_STATE yy_scan_buffer(char *, size_t); But the same problem also, how to do it, thanks, really appreciate your help

Case-insensitive keyword matching

扶醉桌前 提交于 2019-12-07 13:44:43
问题 I'm writing a grammar for parsing a computer language, that can be used with Parse::Eyapp. This is a Perl package that simplifies writing parsers for regular languages. It is similar to yacc and other LALR parser generators, but has some useful extensions, like defining tokens in terms of regular expressions. The language I want to parse uses keywords to denote sections and describe control flow. It also supports identifiers that serve as placeholders for data. An identifier can never have

Python PLY zero or more occurrences of a parsing item

馋奶兔 提交于 2019-12-07 13:01:10
问题 I am using Python with PLY to parse LISP-like S-Expressions and when parsing a function call there can be zero or more arguments. How can I put this into the yacc code. This is my function so far: def p_EXPR(p): '''EXPR : NUMBER | STRING | LPAREN funcname [EXPR] RPAREN''' if len(p) == 2: p[0] = p[1] else: p[0] = ("Call", p[2], p[3:-1]) I need to replace "[EXPR]" with something that allows zero or more EXPR's. How can I do this? 回答1: How about this: EXPR : NUMBER | STRING | LPAREN funcname

What is the meaning of yytext[0]?

十年热恋 提交于 2019-12-07 10:33:11
问题 What is the meaning of yytext[0]? And why should we use in the lex and yacc program? I'm learner so don't mind if it is a silly question. 回答1: yytext holds the text matched by the current token. So yytext[0] holds the first character of the text matched by the current token. Sometimes you have a rule which can match different texts so you need to get the real text matched like for variable names or you have a rule to match all arithmetic operations. A good source is the Flex manual. For

What is the best way of preventing memory leaks in a yacc-based parser?

不羁的心 提交于 2019-12-07 07:49:40
问题 Yacc does not permit objects to be passed around. Because the %union can only contain POD types, complex objects must be new'd and passed around by pointer. If a syntax error occurs, the yacc parser just stops running, and references to all of those created objects are lost. The only solution I've come up with is that all new'd object inherit a particular base class, be added to a container when allocated, and if there is an error everything in that container can be deleted. Does anyone know

y.tab.c: undefined reference to yylex

ε祈祈猫儿з 提交于 2019-12-07 04:29:21
问题 I am trying to run an example I found online of a calculator. But I have this error showing every time I run my gcc command. Here are the commands that I run: flex -l calc3.l yacc -vd calc3.y gcc y.tab.c -lm -ll -> at this point I got this error message: /tmp/ccPOq58f.o : In function 'yyparse': y.tab.c: undefined reference to 'yylex' collect2: error: ld returned 1 exit status Here is my code: calc3.l %{ #include <stdlib.h> #include "calc3.h" #include "y.tab.h" void yyerror(char *); %} %% [a-z

Multiple flex/bison parsers

可紊 提交于 2019-12-07 03:19:26
问题 What is the best way to handle multiple Flex/Bison parsers inside a project? I wrote a parser and now I need a second one in the same project. So far in the third section of parser1.y I inserted the main(..) method and called yyparse from there. What I want to obtain is having two different parsers ( parser1.y and parser2.y ) and be able to use them from an external function (let's assume main in main.cpp ). Which precautions should I use to export yyparse functions outside .y files and how

How can I send the yyleng of a matched string from Lex to Yacc?

我只是一个虾纸丫 提交于 2019-12-07 02:46:31
Please i am trying to pass the yyleng of a matched string from my (.l) file to the (.y) file. Here is a sample of the issue: In the Lex File: <state1>.+ { fprintf(yyout, "%d", yyleng); } In the Yacc File: /* I need to know the methodology used to receive a specific yyleng to the yacc file. Shall I use global variables? or there is a specific way for dealing with this issue? */ Thanks in advance for your help! ~ Any suggestions are highly appreciated. yyleng is a global variable; declare it in your grammar file and use it. Flex uses: typedef size_t yy_size_t; extern yy_size_t yyleng; Lex uses:

Add error checking via production rules to LALR(1) grammar to handle all inputs

北慕城南 提交于 2019-12-06 08:05:58
I have a grammar that represents expressions. Let's say for simplicity it's: S -> E E -> T + E | T T -> P * T | P P -> a | (E) With a , + , * , ( and ) being the letters in my alphabet. The above rules can generate valid arithmetic expressions containing parenthesis, multiplication and addition using proper order of operations and associativity. My goal is to accept every string, containing 0 or more of the letters of my alphabet. Here are my constraints: The grammar must "accept" all strings contained 0 or more letters of my alphabet. New terminals may be introduced and inserted into the