0

I am trying to parse the following string input using Lex and Yacc with no success.

"@user;some random text; @another user; some other random text"

I am using the following grammar:

/* Lambda calculus grammar by Zach Carter */

%lex
%%

\s*\n\s*  {/* ignore */}
";"       { return 'SEP'; }
"@"       { return 'AT'; }
[a-zA-Z]+ { return 'VAR'; }
<<EOF>>   { return 'EOF'; }
/lex

%%

file
  : expr EOF
    { return $expr; }
  | EOF
  ;

expr
  : AT expression  
  | expression
  | SEP expression
  ;
expression
  : VAR
    { $$ = yytext; }
  ;

You can give this grammar a try here:

The result I i would like to have is:

"@user;some random text; @another user; some other random text"

Output:

user some

random text

another user

some other random text

Community
  • 1
  • 1
GETah
  • 20,922
  • 7
  • 61
  • 103
  • "You can give this grammar a try here". Er... Nope, because `AT is not defined`. – Louis Feb 04 '14 at 23:22
  • @Louis That's one of the problems I got. If you look at the lex keywords, `"@" { return AT; }` is well defined – GETah Feb 04 '14 at 23:42
  • Knowing yacc/lex, bison/flex helps using jison but jison is not a drop in replacement for bison (or yacc, or anything else). Jison is its own thing. So things that work with these other tools won't work with jison, including `return AT`. What's puzzling is that you use `return 'VAR'`, not `return VAR` so why `return AT`? – Louis Feb 04 '14 at 23:51
  • Ah well spotted. That's fixed but it still does not work :( – GETah Feb 05 '14 at 18:42

1 Answers1

1

"no success" or "does not work" is not as helpful as an exact description of what's gone wrong would be. Is your "output" the wrong result, or what you want to see (if the latter, then the suggestion that "user some" should be a single entity is a little weird)? Hard to guess what the structure of your language is really intended to be from your question, and the lack of any recursive structure in your grammar suggests the problem could be anything from "can't create a grammar the generator will accept" to "wrong output". So, this answer is probably wrong, but maybe it will elicit a better description.

/* Lambda calculus grammar by Zach Carter */

%lex
%%

\s*\n\s*  /* ignore */
\s+       /* ignore */
";"       { return 'SEP'; }
"@"       { return 'AT'; }
[a-zA-Z]+ { return 'VAR'; }
<<EOF>>   { return 'EOF'; }
/lex

%%

file
  : things EOF
    { return $things; }
  | EOF
  ;
things  // can there be zero things?
  : thing morethings
  ;
morethings
  : SEP things
  |
  ;
thing
  : AT VAR
  | text
  ;
text
  : VAR moretext
  ;
moretext
  : VAR moretext
  |
  ;    
Ron Burk
  • 6,058
  • 1
  • 18
  • 20