flex-lexer

Difficulties during the compilation (g++, bison, flex) with yyparse();

放肆的年华 提交于 2019-12-05 14:31:45
I have a problem with compilation of my code: Flex: %{ #include "lista4.tab.hpp" #include <stdlib.h> extern int yylex(); %} %% "=" {return EQ;} "!=" {return NE;} "<" {return LT;} ">" {return GT;} ":=" {return ASSIGN;} ";" {return SEMICOLON;} "IF" {return IF;} "THEN"{return THEN;} "END" {return END;} [_a-z]+ {yylval.text = strdup(yytext); return IDENTIFIER;} [ \t]+ [0-9]+ { yylval.var = atoi (yytext); return NUMBER; } [-+/^*'%'()] { return *yytext; } \n return RESULT; %% Bison: %{ extern "C" { int yyparse(); int yylex(void); void yyerror(char *s){} int yywrap(void){return 1;} } #include

How is maximal-munch implemented?

China☆狼群 提交于 2019-12-05 08:44:43
I am studying compilers and am learning about lexical analysis. I understand that one specifies each lexeme as a regular expression, and using flex , a lexer can be automatically generated. I am further learning about how the regular expression is converted to an NFA which is then converted to a DFA, where it can be quickly evaluated. However, my question is, how is the maximal-munch rule implemented? Internally, how does the lexer know to "keep going" to find the longest possible lexeme? Thanks! The maximal munch algorithm is implemented by adding a small amount of mutable state to the DFA

How to fix a missing ld library for -lfl while compiling?

你说的曾经没有我的故事 提交于 2019-12-05 02:10:30
I am trying to translate my .spl file into a C file (because there is no compiler). I have an example "Hello World" .spl file, and I have downloaded the Shakespeare Programming Language .tar and extracted it, but I have no idea what to do next. I can't seem to find instructions in any documentation. Can anyone help? Edit: When I type make -f "Makefile" , I get the following output: bison --verbose -d grammar.y gcc -O2 -Wall -c grammar.tab.c gcc -O2 -Wall -c makescanner.c gcc makescanner.o -O2 -Wall -o makescanner ./makescanner include > scanner.l flex -Cem -t scanner.l > scanner.c scanner.l

How do I write a non-greedy match in LEX / FLEX?

我的未来我决定 提交于 2019-12-05 00:37:20
I'm trying to parse a legacy language (which is similar to 'C') using FLEX and BISON. Everything is working nicely except for matching strings. This rather odd legacy language doesn't support quoting characters in string literals, so the following are all valid string literals: "hello" "" "\" I'm using the following rule to match string literals: \".*\" { yylval.strval = _strdup( yytext ); return LIT_STRING; } Unfortunately this is a greedy match, so it matches code like the following: "hello", "world" As a single string ( hello", "world ). The usual non-greedy quantifier .*? doesn't seem to

Why are multi-line comments in flex/bison so evasive?

假如想象 提交于 2019-12-04 23:33:57
I'm trying to parse C-style multi-line comments in my flex (.l) file: %s ML_COMMENT %% ... <INITIAL>"/*" BEGIN(ML_COMMENT); <ML_COMMENT>"*/" BEGIN(INITIAL); <ML_COMMENT>[.\n]+ { } I'm not returning any token and my grammar (.y) doesn't address comments in any way. When I run my executable, I get a parse error: $ ./a.out /* abc def Parse error: parse error $ echo "/* foo */" | ./a.out Parse error: parse error (My yyerror function does a printf("Parse error: %s\n"), which is where the first half of the redundant error message comes from). I can see why the second example fails since the entirety

Making bison/flex parser reentrant with integral YYSTYPE

送分小仙女□ 提交于 2019-12-04 18:23:23
I'm having trouble following the steps to make my bison/flex parser reentrant with a minimum amount of fuss. The problem appears to be in the lexer. Since everything parser is re-entrant, I can no longer assign yylval directly. Instead, according to the Flex manual , I have to call this function: void yyset_lval ( YYSTYPE * yylvalp , yyscan_t scanner ); But the problem is, YYSTYPE is an integral type. It isn't a dynamically allocated value, and it isn't an lvalue at all, so I can't pass a pointer to it! Am I missing something, and if not, how am I supposed to set yylvalue? I've never had this

Flex/Bison IDE? [closed]

偶尔善良 提交于 2019-12-04 18:07:38
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I'm looking for a good development environment in which to work on flex or bison or both. Are there any IDE's that have these capabilities and/or are suitable for this? (If not the next most general question is are there lexer/parser generators with IDE's?) Thanks ~Alex 回答1: ANTLR has several IDEs available,

Flex/Bison Error:request for member `str' in something not a structure or union

别说谁变了你拦得住时间么 提交于 2019-12-04 17:56:17
I'm learning flex/bison. I wrote the following program but getting errors. %{ #include <stdio.h> typedef struct node { struct node *left; struct node *right; char *token; }node; node *mknode( node *left, node *right, char *token); void printtree(node *tree); #define YYSTYPE struct node * %} %union { char* str; int num; } %start lines %token <str> WORD %token <str> PLUS MINUS TIMES DIVIDE POWER %token <str> LEFT_PARENTHESIS RIGHT_PARENTHESIS %token <str> END %left PLUS MINUS %left TIMES DIVIDE %right POWER %type <str> exp term factor line lines %% lines: /* empty */ | lines line; line: exp END

Flex/Bison-like functionality within PHP

百般思念 提交于 2019-12-04 17:52:08
问题 I'm looking for a way to get Flex/Bison (or Lex/Yacc, et. al.) support in PHP. Specifically, I'm implementing a boolean query parser in a web UI and would rather keep all operations inside of PHP (as opposed to calling a C parser, or passing things off to Python, etc.). 回答1: LIME Parser Generator for PHP: Complete LALR(1) parser generator and engine (like BISON or YACC) but it's all done in PHP, and the input grammar is easier and more maintainable. Write your actions in PHP. Generate PHP

unistd.h related difficulty when compiling bison & flex program under vc++

谁说我不能喝 提交于 2019-12-04 15:27:48
问题 I'm using bison & flex (downloaded via cygwin) with vc++. When I compile the program I got an error: ...: fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory The corresponding code in the flex-generated file is: #ifndef YY_NO_UNISTD_H /* Special case for "unistd.h", since it is non-ANSI. We include it way * down here because we want the user's section 1 to have been scanned first. * The user has a chance to override it with an option. */ /* %if-c-only */