yacc

semantic type checking analysis in bison

岁酱吖の 提交于 2019-12-06 07:18:14
问题 I've been trying to find examples everywhere but it's been in vain. I am trying to write a basic Ruby interpreter. For this, I wrote a flex lexical file, containing token recognition sentences, and a grammar file. I wish for my grammar to contain semantic type checking. My grammar file contains, for example: arg : arg '+' arg This should be a valid rule for integers and floats. According to what I've read, I can specify type for a non terminal such as arg, like so: %type <intval> arg where

How to resolve a shift/reduce conflict forcing a shift or a reduce?

别等时光非礼了梦想. 提交于 2019-12-06 01:08:39
When there is a shift/reduce conflict in Yacc/Bison, is it possible to force the conflict to be solved exactly as you want? In other words: is it possible explicitly force it to prioritize the shift or the reduce? For what I have read, if you are happy with the default resolution you can tell the generator to not complain about it . I really don't like this because it is obfuscating your rational choice. Another option is to rewrite the grammar to fix the issue. I don't know if this is always possible and often this makes it much harder to understand. Finally, I have read the precedence rules

Python PLY zero or more occurrences of a parsing item

巧了我就是萌 提交于 2019-12-06 01:08:14
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? How about this: EXPR : NUMBER | STRING | LPAREN funcname EXPR_REPEAT RPAREN EXPR_REPEAT: /*nothing*/ | EXPR EXPR_REPEAT Are you sure you want a Context Free Grammar and

Why is yylval null?

拈花ヽ惹草 提交于 2019-12-06 01:00:11
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. Also, I tried initializing yylval in main of test.y, but yylval = malloc(...) gives a type mismatch-- as

Case-insensitive keyword matching

时光怂恿深爱的人放手 提交于 2019-12-05 21:52:04
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 the same name as a keyword. Now, here comes the tricky part: I need to separate keywords from

Is there a Sublime Text Syntax for Flex and Bison?

梦想与她 提交于 2019-12-05 15:09:26
问题 I'm looking for a syntax in Sublime Text that highlights my Flex and Bison files (or lex/yacc) in a way that makes them readable... Sublime Text automatically chooses Lisp for Flex files, but that doesn't do the trick all that well. Any suggestions to try another syntax? Or is there a plugin somewhere that's useful (haven't found anything so far)?. 回答1: I haven't found one built specifically for Sublime, but I've found one for TextMate, which Sublime is compatible with. Therefore, for Flex

y.tab.c: undefined reference to yylex

泪湿孤枕 提交于 2019-12-05 09:28:50
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] { yylval.sIndex = *yytext - 'a'; return VARIABLE; } 0 { yylval.iValue = atoi(yytext); return INTEGER;

Multiple flex/bison parsers

旧巷老猫 提交于 2019-12-05 06:44:35
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 should I handle two parsers? PS. I'm using g++ to compile but not the C++ versions of Flex and Bison and

Why does this simple grammar have a shift/reduce conflict?

别等时光非礼了梦想. 提交于 2019-12-05 06:14:42
问题 %token <token> PLUS MINUS INT %left PLUS MINUS THIS WORKS: exp : exp PLUS exp; exp : exp MINUS exp; exp : INT; THIS HAS 2 SHIFT/REDUCE CONFLICTS: exp : exp binaryop exp; exp : INT; binaryop: PLUS | MINUS ; WHY? 回答1: This is because the second is in fact ambiguous. So is the first grammar, but you resolved the ambiguity by adding %left . This %left does not work in the second grammar, because associativity and precedence are not inherited from rule to rule. I.e. the binaryop nonterminal does

Assigning multiple data types to a non-terminal in yacc

我怕爱的太早我们不能终老 提交于 2019-12-05 06:08:09
问题 I'm working on a project for class in which we have to build a parser. We're currently in the stage of building the parser in yacc. The thing currently confusing me is I've read that you need to assign a type to each nonterminal. In some cases though I'll have Something like: ... %union { Type dataType; int integerConstant; bool boolConstant; char *stringConstant; double doubleConstant; char identifier[MaxIdentLen+1]; // +1 for terminating null Decl *decl; List<Decl*> *declList; } %token