yacc

A parser program for the following grammar

浪子不回头ぞ 提交于 2019-12-11 06:57:17
问题 Write a parser (both Yacc and Lex files) that uses the following productions and actions: S -> cSS {print “x”} S -> a {print “y”} S -> b {print “z”} Indicate the string that it will print when the input is cacba . I am getting this error: when I give input to it, it says valid input and also says syntax error. My Scanner Code is this %{ #include "prac.h" %} %% [c] {return C; } [a] {return A; } [b] {return B; } [ \t] ; \n { return 0; } . { return yytext[0]; } %% int yywrap(void) { return 1; }

Call a function in a Yacc file from another c file

自古美人都是妖i 提交于 2019-12-11 04:36:40
问题 I am new in Lex/Yacc programming. I have a question about how to call a function in Yacc file from another C file. Assume that I have following Lex/Yacc code: calc.l %{ #include "y.tab.h" extern int yylval; %} %% [0-9]+ { yylval=atoi(yytext); return NUMBER;} [ \t]; \n return 0; . return yytext[0]; %% calc.y %{ #include <stdio.h> %} %token NAME NUMBER %% statement: NAME '=' expression | expression {printf("= %d\n",$1); printf("yylval= %d",yylval);} ; expression: NUMBER '+' NUMBER {$$=$1+$3;} |

Basic problem with yacc/lex

梦想的初衷 提交于 2019-12-11 02:25:52
问题 I have some problems with a very simple yacc/lex program. I have maybe forgotten some basic steps (it's been a long time since I've used these tools). In my lex program I give some basic values like : word [a-zA-Z][a-zA-Z]* %% ":" return(PV); {word} { yylval = yytext; printf("yylval = %s\n",yylval); return(WORD); } "\n" return(ENDLINE); In my yacc program the beginning of my grammar is (where TranslationUnit is my %start) : TranslationUnit: /* Nothing */ | InfoBlock Data ; InfoBlock: /*

Yacc and Lex inclusion confusion

送分小仙女□ 提交于 2019-12-11 01:48:01
问题 I am wondering how to correctly compile a program with a Makefile that has calls to yyparse in it? This is what I do: I have a Makefile that compiles all my regular files and they have no connections to y.tab.c or lex.yy.c (Am I supposed to have them?) I do this on top of my code: #include "y.tab.c" #include "lex.yy.c" #include "y.tab.h" This is what happens when I try to make the program: When I just type in "make", it gives me many warnings. Some of the examples are shown below. In function

Bison token is rest of the string

可紊 提交于 2019-12-11 01:31:27
问题 I have written a flex and bison, I am facing a problem which is illustrated via the below program. The program is intended to parse the key-value pairs separated by an equals (=) sign I am hoping that my bison script tokenizes the key and values and prints them. Below is the snippet of my flex program %{ /* file : kvp.l */ #include <stdio.h> #define YYSTYPE char* #include "kvp.tab.h" %} %% [a-zA-Z0-9][_a-zA-Z0-9]* { yylval=yytext; return IDENTIFIER; } "=" { yylval=yytext; return EQUALS_OP; }

bison/lex YYSTYPE declaration as struct

孤者浪人 提交于 2019-12-11 01:14:57
问题 I've been trying for a while, to implement a parser for a grammar by using bison and lex. I have a problem with the type redeclaration of yylval, I explain myself. I have 4 files: lexico.l, parser.y, funcionesTabla.c, funcionesTabla.h The first, contains the specification for lex The second, specification for bison/yacc The last two, are a bunch of methods for dealing with a symbol table. I have in funcionesTabla.h: typedef enum { entero, real, caracter, arrayEntero, arrayReal, arrayCaracter,

Parser for xml DTD file

谁说胖子不能爱 提交于 2019-12-11 00:36:42
问题 I am quite new in implementing a parser and I am trying to pars a xml DTD file to generate a context free grammar for it. I tried pyparsing and yacc but still I could get any result. So I would appreciate if some one could provide me some tips or sample code to write such a parser. following is a sample DTD file: <!DOCTYPE PcSpecs [ <!ELEMENT PCS (PC*)> <!ELEMENT PC (MODEL, PRICE, PROCESSOR, RAM, DISK+)> <!ELEMENT MODEL (\#PCDATA)> <!ELEMENT PRICE (\#PCDATA)> <!ELEMENT PROCESSOR (MANF, MODEL,

Representing statement-terminating newlines in a grammar?

让人想犯罪 __ 提交于 2019-12-10 18:36:19
问题 A lot of programming languages have statements terminated by line-endings. Usually, though, line endings are allowed in the middle of a statement if the parser can't make sense of the line; for example, a = 3 + 4 ...will be parsed in Ruby and Python* as the statement a = 3+4 , since a = 3+ doesn't make any sense. In other words, the newline is ignored since it leads to a parsing error. My question is: how can I simply/elegantly accomplish that same behavior with a tokenizer and parser? I'm

lex and yacc (symbol table generation)

一世执手 提交于 2019-12-10 17:11:44
问题 I am new to lex and yacc and compiler design. I would like to know at which phase(lexical, syntactical or any other phase) and how the symbol table is generated? Can I have a brief description of y.output file which is generated by giving -v option to yacc.I tried to looking into it but didn't get much info. Could I know the other applications where lex and yacc are used apart from compiler designs. 回答1: A symbol table is a global data structure that can be used in all stages/phases/passes of

Tracking source position of AST nodes in a compiler (ocaml)

▼魔方 西西 提交于 2019-12-10 15:57:03
问题 I'm writing a compiler in ocaml, using ocamllex/yacc. Things are going well, but I've a design problem. For each AST node I create, it'd be good to have information about line/character position of that node in the source code. That would be useful for providing error messages to the user later. Now, I can add some kind of meta type to my nodes: type node = Node1 of ... * meta | Node2 of ... * meta but that seems redundant. Later, when I'm done with verifying the AST, I'll have to write match