flex-lexer

How can I rewrite the programs, so that I don't have to call `flex` but only call `bison` and `cc`?

做~自己de王妃 提交于 2019-12-11 23:23:48
问题 I already have a calculator program based on bison and flex which takes input from command line arguments. Now how can I rewrite the programs, so that I don't have to call flex but only call bison and cc during building process? (Achieve something similar to https://unix.stackexchange.com/questions/499190/where-is-the-official-documentation-debian-package-iproute-doc#comment919875_499225). $ ./fb1-5 '1+3' = 4 Makefile: fb1-5: fb1-5.l fb1-5.y bison -d fb1-5.y flex fb1-5.l cc -o $@ fb1-5.tab.c

Remove default YY_DECL from Flex output

心已入冬 提交于 2019-12-11 15:38:04
问题 I was able to sidestep this problem when manually setting up the order in which header files are read by the compiler. In this case, I'm able to spoof the default "pearl" with the correct definition, but when I have no control of the order in which headers are included, this... genius of engineering surfaces: /* Default declaration of generated scanner - a define so the user can * easily add parameters. */ #ifndef YY_DECL #define YY_DECL_IS_OURS 1 /* %if-c-only Standard (non-C++) definition *

How to reference lex or parse parameters in flex rules?

倖福魔咒の 提交于 2019-12-11 12:56:18
问题 I know I can declare %parse-param {struct my_st *arg} in a .y file. And so yyparse() is changed to be yyparse(struct my_st *arg) . But how do I reference the argument in the flex rules? For example: [0-9]+ { do_work(arg); return NUMBER; } I want to make a reentrant parser, so I need to do this. Please help me, thanks! 回答1: You need to pass the argument through to yylex . That requires a modification of both the bison parser description, so that the parser calls yylex with the desired

Windows cannot find win_bison.exe

北战南征 提交于 2019-12-11 10:13:38
问题 I'm trying to get to work with flex and bison in Visual Studio 2013, but I have a problem. I've downloaded win_flex_bison from here, followed this tutorial and added "C:\GnuWin32\win_flex_bison;" to environmental variables, but when I'm trying to build the project I'm getting error that Windows cannot find win_bison.exe "Error 1 error MSB3721: The command "start /B /WAIT /D "D:\\...\" win_bison.exe --output="sample.tab.cpp" --defines="sample.tab.h" "sample.y" " exited with code 1. C:\GnuWin32

can't access the token table yytname in bison 2.6 or is it gone?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 09:56:55
问题 I'm building a parser for an asset xchange format. And I'm including the %token-table directive in the bison file but, from the flex code I just can't access the table or the constants associated with it. That is when trying to compile this code: Frame|FrameTransformMatrix|Mesh|MeshNormals|MeshMaterialList|Material { printf("A keyword: %s\n", yytext); yylval.charptr_type = yytext; int i; for (i = 0; i < YYNTOKENS; i++) { if (yytname[i] != 0 && yytname[i][0] == '"' && !strncmp(yytname[i] + 1,

Matching trailing context in flex

百般思念 提交于 2019-12-11 08:55:17
问题 In the flex manual it mentions a "trailing context" pattern ( r/s ), which means r , but only if followed by s . However the following code doesn't compile (instead it gives an error of "unrecognized rule". Why? LITERAL a/b %% {LITERAL} { } 回答1: The simple answer is that unless you use the -l option, which is not recommended, you cannot put trailing context into a name definition. That's because flex: doesn't allow trailing context inside parentheses; and automatically surrounds expansions of

Beginner bison flex

一曲冷凌霜 提交于 2019-12-11 07:39:37
问题 How can I print the line number on which an error occurred. I tried using yylineno in the yyerror() function and writing %option yylineno in the .l file but after compiling it gives me an error " yylineno undeclared (first use in this function) " and if I initialize yylineno as 1 it gives me this error: error: redefinition of yylineno lex.yy.c:273: note: previous definition of yylineno was here 回答1: There is a second way to ask flex to provide the global variable yylineno: the command line

lex program on counting no of comment lines

旧街凉风 提交于 2019-12-11 07:30:12
问题 here the program counts the no of comment lines, single line comments and multi line comments and gives a total comments output with a file.txt as input file.txt //hellow world /*hello world1*/ /*hello world2 */ /*hello world3 hello world3.1*/ #include<> count.l %{ #include<stdio.h> #include<stdlib.h> int a=0,b=0,c=0,d; %} %% "//".* {a++;} "/*" {b++;} .*"*/" {b--;c++;} %% void main(int argc,char *argv[]){ yyin=fopen(argv[1],"r"); yylex(); printf("single line %d \nmultiline %d \n",a,c); d=a+c;

How does Flex distinguish between A, AB, and ABC?

自作多情 提交于 2019-12-11 06:07:26
问题 I made this experiment for Flex to see if I enter ABC, if it will see all A, AB, ABC or only ABC or only the first match in the list of expressions. %{ #include <stdio.h> %} %% A puts("got A"); AB puts("got AB"); ABC puts("got ABC"); %% int main(int argc, char **argv) { yylex(); return 0; } When I enter ABC after compiling and running the program, it responds with "Got ABC" which really surprises me since I thought lex doesn't keep track of visited text, and only finds the first match; but

Scanner and parser interaction

爱⌒轻易说出口 提交于 2019-12-11 05:20:00
问题 I am new to flex/bison. Reading books, it seems that in nearly all compiler implementations, the parser interacts with the scanner in a "coroutine" manner, that whenever the parser needs a token, it calls the scanner to get one, and left the scanner aside when it's busy on shift/reduce. A natural question is that why not let the scanner produces the token-stream (from the input byte-stream) as a whole, and then pass the entire token-stream to the parser, thus there is no explicit interaction