lex

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;} |

How do I lex unicode characters in C?

此生再无相见时 提交于 2019-12-11 03:17:56
问题 I've written a Lexer in C, it currently lexes files in ASCII successfully, however I'm confused as to how I would lex unicode. What unicode would I need to lex, for instance should I support utf-8, utf-16, etc. What do languages like Rust or Go support? If so are there any libraries that can help me out, although I would prefer to try and do it myself so I can learn. Even then, a small library that I could read to learn from would be great. 回答1: There are already version of lex (and other

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/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,

segmentation fault with bison and flex

ⅰ亾dé卋堺 提交于 2019-12-11 00:54:49
问题 I was trying learn lex and yacc using the oreilly book. I tried following example from book, but it gives segmentation fault. %{ /** * A lexer for the basic grammar to use for recognizing English sentences. */ #include <stdio.h> extern FILE *yyin; %} %token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION %% sentence: subject VERB object{ printf("Sentence is valid.\n");} ; subject: NOUN | PRONOUN ; object: NOUN ; %% main() { while(!feof(yyin)) { yyparse(); } } yyerror(char *s) {

Differentiating division from regex when lexing gawk code

血红的双手。 提交于 2019-12-11 00:32:19
问题 I am writing a flex parser for gawk scripts. I am running into a problem differentiating between uses for a forward slash (/) character. Obviously, a single / would be an operator for division, but two slashes could be both a regular expression or division. Right now, it parses int((r-1)/3)*3+int((c-1)/3)+1 as having the regular expression /3)*3+int((c-1)/ instead of the intended division operations. How do I get flex to recognize it as a mathematical expression? Right now, this is my flex

Regex clarification on escape sequences with lex

此生再无相见时 提交于 2019-12-10 23:19:10
问题 I'm creating a lexer.l file that is working as intended except for one part. I have the rule: [\(\*.*\*\)] {} which I want to make it so when I encounter (* this is a test *) in a file, I simply do nothing with it. However when I run lex lexer.l I get warning on lines with rules \( , \* , and \) stating that they can never be met. So I guess my question is why would [\(\*.*\*\)] {} interfere with \( and the others? How can I catch (* this is a test *) ? 回答1: Languages with the comment syntax

lex : How to override YY_BUF_SIZE

这一生的挚爱 提交于 2019-12-10 21:27:18
问题 According to the manual YY_BUF_SIZE is 16K and we need to override it. However, the manual does not specify how to override it, nor could I find any command line option for this. Can someone please indicate how to change this. In the generated source YY_BUF_SIZE is defined as follows: #ifndef YY_BUF_SIZE #define YY_BUF_SIZE 16384 #endif so there may be a way to override it before this. 回答1: In your own code, simply #define YY_BUF_SIZE to whatever value you want. As long as you compile your

Flex/Bison EOF propagation from stdin vs a file

痞子三分冷 提交于 2019-12-10 19:28:13
问题 I have a scanner, parser and a main from which I create an executable via bison -d parser.y; flex scanner.l; gcc main.c parer.tab.c lex.yy.c When I run ./a.out it does what I want: If Ctrl+D is pressed an EOF is detected and main can act accordingly. This means: if yyin is stdin then hitting Return ends the parsing of that line and the main loop waits for the next input line. Pressing Ctrl+D ends parsing input with a break in the main loop and exits. If the input comes from a file, e,g,

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