yacc

Yacc: Code after each non-terminal

我只是一个虾纸丫 提交于 2019-12-13 02:51:27
问题 Is there a way to execute code after each Terminal? So that something like this is possible: a : B { code } C { some code } Any help would really be appreciated! 回答1: The example you gave should work as is. 回答2: You are right. I some how didn't get that C is $3 after inserting the code and not $2... really odd. 来源: https://stackoverflow.com/questions/393104/yacc-code-after-each-non-terminal

Bison: how to fix reduce/reduce conflict

早过忘川 提交于 2019-12-13 02:40:51
问题 Below is a a Bison grammar which illustrates my problem. The actual grammar that I'm using is more complicated. %glr-parser %% s : e | p '=' s; p : fp | p ',' fp; fp : 'x'; e : te | e ';' te; te : fe | te ',' fe; fe : 'x'; Some examples of input would be: x x = x x,x = x,x x,x = x;x x,x,x = x,x;x,x x = x,x = x;x What I'm after is for the x's on the left side of an '=' to be parsed differently than those on the right. However, the set of legal "expressions" which may appear on the right of an

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

给你一囗甜甜゛ 提交于 2019-12-12 08:16:43
问题 %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

Compiling and executing the Shakespeare Programming Language translator spl2c on Mac OS X 10.6 results in warnings/errors

六眼飞鱼酱① 提交于 2019-12-12 07:56:40
问题 I wanted to experiment with the Shakespeare programming language, so I downloaded it from here and executed the Makefile using cd spl-1.2.1 Make . The compilation of spl2c executes with a couple warnings: scanner.l:600: warning, rule cannot be matched <stdout>:5808: warning: ‘yyunput’ defined but not used And then when it attempts to compile all the examples everything goes haywire: ../spl/bin/spl2c < fibonacci.spl > fibonacci.c Warning at line 19: equality expected Warning at line 28:

Is it possible to call one yacc parser from another to parse specific token substream?

若如初见. 提交于 2019-12-12 06:26:57
问题 Suppose I already have a complete YACC grammar. Let that be C grammar for example. Now I want to create a separate parser for domain-specific language, with simple grammar, except that it still needs to parse complete C type declarations. I wouldn't like to duplicate long rules from the original grammar with associated handling code, but instead would like to call out to the original parser to handle exactly one rule (let's call it "declarator"). If it was a recursive descent parser, there

Shift/Reduce conflict on C variation grammar

给你一囗甜甜゛ 提交于 2019-12-12 05:25:26
问题 I am writing a parser to a C-like grammar, but I am having a problem with a shift/reduce conflict: Basically, the grammar accept a list of optional global variables declarations followed by the functions. I have the following rules: program: global_list function_list; type_name : TKINT /* int */ | TKFLOAT /* float */ | TKCHAR /* char */ global_list : global_list var_decl ';' | ; var_decl : type_name NAME; function_list : function_list function_def | ; function_def : type_name NAME '(' param

Meaning of “<*>” in lex

只愿长相守 提交于 2019-12-12 05:23:15
问题 I know,we can define some conditions in lex, matching: 1.<DIRECTIVE>{STRING} {printf("Matching the DIRECTIVE state!");} 2.<REFERENCE>{INTEGER} {printf("Matching the REFERNCE state!");} 3.[\n] {printf("Matching the INITIAL state?");} 4.<*>{DOBULE} {printf("Matching all state include INITIAL? Seem not!");} How to use the states in the right way? What is the difference in conditions on line 3 and 4? The whole .l file, cut by me,now it just to realize a reference.When I run it,it can work well

why does $1 in yacc/bison has a value of 0

[亡魂溺海] 提交于 2019-12-12 03:06:25
问题 I have a following production in a bison spec: op : '+' { printf("%d %d %c\n", $1, '+', '+'); } When I input a + I get the following output: 0 43 + Can someone explain why $1 has a value of 0, shouldn't it be 43? What am I missing? EDIT There is no flex file, but I can provide a bison grammar: %{ #include <stdio.h> #include <ctype.h> #include <string.h> int yylex(); int yyerror(); %} %token NUMBER %% lexp : NUMBER | '(' op lexp-seq ')' ; op : '+' { printf("%d %d %c\n", $1, '+', '+'); } | '-'

lex and yacc program to convert infix to prefix

回眸只為那壹抹淺笑 提交于 2019-12-11 23:15:50
问题 I am new to lex and yacc programs.I have been trying to write a yacc program which takes as input an arithmetic expression and gives prefix notation as output. here is my lex code. %{ #include<string.h> #include"pre.tab.h" %} %% "*"|"/"|"+"|"-"|"("|")" {return yytext[0];} [0-9]+ {yylval.name=(char*)malloc(yyleng+1); strcpy(yylval.name,yytext); return NUM;} \n {return(0);} [a-zA-Z][a-zA-Z]* {yylval.name=(char*)malloc(yyleng+1); strcpy(yylval.name,yytext); return ID;} . {} %% int yywrap() {

Lex and Yacc and EBNF specification

五迷三道 提交于 2019-12-11 20:30:56
问题 if I just want to check whether the syntax of a language is correct or not, what is the easy way of writing a syntax analyzer using yacc. 回答1: Note that the ISO standard for EBNF is ISO 14977:1996 and the 'EBNF' you've used in the question bears limited resemblance to the standard version. That leaves us having to interpret your grammar rule. Non-terminals are written as single words in all lower-case. Terminals are written as single words in all upper-case. Colon is used to separate a non