lex

REPL for interpreter using Flex/Bison

对着背影说爱祢 提交于 2019-11-30 09:15:00
I've written an interpreter for a C-like language, using Flex and Bison for the scanner/parser. It's working fine when executing full program files. Now I'm trying implement a REPL in the interpreter for interactive use. I want it to work like the command line interpreters in Ruby or ML: Show a prompt Accept one or more statements on the line If the expression is incomplete display a continuation prompt allow the user to continue entering lines When the line ends with a complete expression echo the result of evaluating the last expression show the main prompt My grammar starts with a top_level

in lex how to make yyin point to a file with the main function in yacc?

笑着哭i 提交于 2019-11-30 08:26:27
问题 I am storing the arguments passed to main in yacc in a file. Now I want the lex to read its input from this file rather than the terminal. I know I can point yyin to a file like yyin = fopen("fn","r"); but this works only when main is in lex. When I use this yyin declaration in main in yacc, it shows an error so please suggest something to overcome this problem. 回答1: You probably just need to declare extern FILE * yyin; If that doesn't solve the problem, please give the error message you got.

How to create a parser(lex/yacc)?

喜你入骨 提交于 2019-11-30 05:57:11
I'm having the following file and which need to be parsed --TestFile Start ASDF123 Name "John" Address "#6,US" end ASDF123 The lines start with -- will be treated as comment lines. and the file starts 'Start' and ends with end . The string after Start is the UserID and then the name and address will be inside the double quots. I need to parse the file and write the parsed data into an xml file. So the resulting file will be like <ASDF123> <Name Value="John" /> <Address Value="#6,US" /> </ASDF123> now i'm using pattern matching( Regular Expressions ) to parse the above file . Here is my sample

Simple Flex/Bison C++

拥有回忆 提交于 2019-11-30 05:04:11
I already looked for my answer but I didn't get any quick response for a simple example. I want to compile a flex/bison scanner+parser using g++ just because I want to use C++ classes to create AST and similar things. Searching over internet I've found some exploits, all saying that the only needed thing is to declare some function prototypes using extern "C" in lex file. So my shady.y file is %{ #include <stdio.h> #include "opcodes.h" #include "utils.h" void yyerror(const char *s) { fprintf(stderr, "error: %s\n", s); } int counter = 0; extern "C" { int yyparse(void); int yylex(void); int

how to install Lex and Yacc in Ubuntu? [closed]

最后都变了- 提交于 2019-11-30 00:19:15
I am doing project in SENSE, for that i have to install Lex and Yacc. If you can help me how to install in Ubuntu. I very new to this area. So can you help me. Any website to study the basic of Lex and Yacc halfdan Use the synaptic packet manager in order to install yacc / lex. If you are feeling more comfortable doing this on the console just do: sudo apt-get install bison flex There are some very nice articles on the net on how to get started with those tools. I found the article from CodeProject to be quite good and helpful ( see here ). But you should just try and search for "introduction

Generating a compiler from lex and yacc grammar

≡放荡痞女 提交于 2019-11-29 19:47:58
问题 I'm trying to generate a compiler so I can pass him a .c file after. I've downloaded both YACC and LEX grammars from http://www.quut.com/c/ANSI-C-grammar-y.html and named them clexyacc.l and clexyacc.y When generating it on terminal I did : yacc -d clexyacc.y lex clexyacc.l All went fine. When I move on to the last part I get a few errors. The last part is : cc lex.yy.c y.tab.c -oclexyacc.exe But I get these errors : y.tab.c:2261:16: warning: implicit declaration of function 'yylex' is

yytext contains characters not in match

房东的猫 提交于 2019-11-29 16:42:40
Background I am using flex to generate a lexer for a programming language I am implementing. I have some problems with this rule for identifiers: [a-zA-Z_][a-zA-Z_0-9]* { printf("yytext is %s\n", yytext); yylval.s = yytext; return TOK_IDENTIFIER; } The rule works as it should when my parser is parsing expressions like this: var0 = var1 + var2; The printf statement will print out this: yytext is 'var0' yytext is 'var1' yytext is 'var2' Which is what it should. The problem But when my parser is parsing function declarations like this one: func(array[10] type, arg2 wef, arg3 afe); Now the printf

Lex - How to run / compile a lex program on commandline

孤人 提交于 2019-11-29 14:55:00
问题 I am very new to Lex and Yacc. I have a Lex program. Example: wordcount.l I am using windows and putty. I am just trying to run this file.. Does the wordcount.l file go on the C drive? Do I compile the Lex program and it generates a .c program and then what do I run? I tried on the command-line: Lex wordcount.l but I just get file not found... wordcount.l %{ #include <stdlib.h> #include <stdio.h> int charCount=0; int wordCount=0; int lineCount=0; %} %% \n {charCount++; lineCount++;} [^ \t\n]+

how to parse from a string rather than a file [duplicate]

纵然是瞬间 提交于 2019-11-29 12:59:50
Possible Duplicate: How to make YY_INPUT point to a string rather than stdin in Lex & Yacc (Solaris) i want to parse from a string rather than a file. i know that v can use yy_scan_string fn to do it.but for me it's not working properly so pls help me I fought through this problem myself very recently. The flex documentation on the subject leaves a bit to be desired. I see two things right off the bat that might be tripping you up. First, note that your string needs to be double NULL terminated . That is, you need to take a regular, NULL terminated string and add ANOTHER NULL terminator at the

How to use Boost::Spirit::Lex to lex a file without reading the whole file into memory first?

坚强是说给别人听的谎言 提交于 2019-11-29 10:44:59
I'm looking at writing a lexer using boost::spirit::lex, but all the examples I can find seem to assume that you've read the entire file into RAM first. I'd like to write a lexer that doesn't require the whole string to be in RAM, is that possible? Or do I need to use something else? I tried using istream_iterator, but boost gives me a compile error unless I use const char* as the iterator types. e.g. All the examples I can find basically do this: lex_functor_type< lex::lexertl::lexer<> > lex_functor; // assumes entire file is in memory char const* first = str.c_str(); char const* last =