lex

lexical analysis stops after yy_scan_string() is finished

拥有回忆 提交于 2019-12-12 03:34:59
问题 I use flex to make a lexical analyzer. I want to analyse some define compiler statements which are in the form: #define identifier identifier_string. I keep a list of (identifier identifier_string) pair. So when I reach in the file a identifier that is #define list I need to switch the lexical analysis from main file to analyse the corresponding identifier_string. (I don't put the complete flex code because is too big) here's the part: {IDENTIFIER} { // search if the identifier is in list if(

How to get such pattern matching of regular expression in lex

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 03:08:12
问题 Hi I want to check a specific pattern in regular expression but I'm failed to do that. Input should be like noun wordname : wordmeaning I'm successful getting noun and wordname but couldn't design a pattern for word meaning. My code is : int state; char *meaning; char *wordd; ^verb { state=VERB; } ^adj { state = ADJ; } ^adv { state = ADV; } ^noun { state = NOUN; } ^prep { state = PREP; } ^pron { state = PRON; } ^conj { state = CONJ; } //my try but failed [:\a-z] { meaning=yytext; printf("

Unrecognized rule in lex on Ubuntu 14.04

风格不统一 提交于 2019-12-12 02:05:34
问题 I am attempting to have lex use echo to spit back out reserved words in a program but I continue to get the following errors: scanner.l:30: unrecognized rule scanner.l:30: unrecognized rule scanner.l:70: unrecognized rule scanner.l:70: unrecognized rule scanner.l:70: unrecognized rule scanner.l:71: unrecognized rule scanner.l:71: unrecognized rule Below is my scanner code for lex: %{ #include <stdio.h> #include <ctype.h> #include "tokens.h" %} ws [ \t\r\n]+ quoted \".*\" letter [A-Za-z] digit

Error while parsing comments in lex

☆樱花仙子☆ 提交于 2019-12-12 00:39:26
问题 Lex code: identifier [\._a-zA-Z0-9\/]+ comment "//" <*>{comment} { cout<<"Comment\n"; char c; while((c= yyinput()) != '\n') { } } <INITIAL>{s}{e}{t} { BEGIN(SAMPLE_STATE); return SET; } <SAMPLE_STATE>{identifier} { strncpy(yylval.str, yytext,1023); yylval.str[1023] = '\0'; return IDENTIFIER; } In the above lex code, there is no error when "// set name" is parsed. Please notice the space after "//" in the sentence parsed. However, when "//set name" is parsed, there is an error reported. Could

Is it possible to let several lexers share same ident definitions?

谁说胖子不能爱 提交于 2019-12-11 23:25:42
问题 I have several lexers: lexer_1.mll , lexer_2.mll , ... Some definitions of ident ( let ident = regexp ) are common and repeated in these files. For instance, the definition of INTEGER , FLOAT , ... Does anyone know if it is possible to define them the once for all somewhere, and let the .mll files call it? 回答1: I'm afraid there's no "pure OCaml" solution, as ident seems to be systematically inlined by ocamllex . You can still put your regexp definition in a file, and use cpp (or any other C

lex and yacc program to convert infix to prefix

回眸只為那壹抹淺笑 提交于 2019-12-11 23:15:50
问题 I am new to lex and yacc programs.I have been trying to write a yacc program which takes as input an arithmetic expression and gives prefix notation as output. here is my lex code. %{ #include<string.h> #include"pre.tab.h" %} %% "*"|"/"|"+"|"-"|"("|")" {return yytext[0];} [0-9]+ {yylval.name=(char*)malloc(yyleng+1); strcpy(yylval.name,yytext); return NUM;} \n {return(0);} [a-zA-Z][a-zA-Z]* {yylval.name=(char*)malloc(yyleng+1); strcpy(yylval.name,yytext); return ID;} . {} %% int yywrap() {

Lex and Yacc and EBNF specification

五迷三道 提交于 2019-12-11 20:30:56
问题 if I just want to check whether the syntax of a language is correct or not, what is the easy way of writing a syntax analyzer using yacc. 回答1: Note that the ISO standard for EBNF is ISO 14977:1996 and the 'EBNF' you've used in the question bears limited resemblance to the standard version. That leaves us having to interpret your grammar rule. Non-terminals are written as single words in all lower-case. Terminals are written as single words in all upper-case. Colon is used to separate a non

Grammar construction in ply - recursion allowed?

▼魔方 西西 提交于 2019-12-11 19:51:31
问题 This has maybe been asked before but I do not know what to search for, really. Suppose I have some string I'd like to build a parser with. I have strings like a OR b , b OR C but also a OR (b AND c) . Now the nested parentheses cause trouble for me and I don't know how to construct the appropriate p_* functions. Is recursion allowed? If so, how? This is what I have thus far: import ply.lex as lex import ply.yacc as yacc # List of token names. This is always required tokens = ( 'VARIABLE', 'OR

How to handle multiple rules for one token with PLY

不打扰是莪最后的温柔 提交于 2019-12-11 16:47:49
问题 I'm working with a jison file and converting it to a parser generator using the lex module from python PLY. I've noticed that in this jison file, certain tokens have multiple rules associated with them. For example, for the token CONTENT , the file specifies the following three rules: [^\x00]*?/("{{") { if(yytext.slice(-2) === "\\\\") { strip(0,1); this.begin("mu"); } else if(yytext.slice(-1) === "\\") { strip(0,1); this.begin("emu"); } else { this.begin("mu"); } if(yytext) return 'CONTENT';

Lex/Yacc for Boolean Calculation

五迷三道 提交于 2019-12-11 14:57:04
问题 I'm trying to write a program for calculating a NAND boolean expression using Lex/Yacc. For example, if input is "true nand (false nand true)," my program should print "false" This is what I have so far,, but I am really stuck What am I missing or doing wrong? boolean.l %{ #include "y.tab.h" %} %% "true"|"false" {return BOOL;} "nand" {return NAND;} [()] {return yytext[0];} %% boolean.y %token BOOL NAND %left NAND %% boolexp: boolexp NAND boolterm {$$ = !($1 && $3);} | boolterm {$$=$1;} ;