librelist archives

« back to archive

yytext in PHP jison

yytext in PHP jison

From:
Karel Kozlik
Date:
2012-04-17 @ 16:33
  Hello guys,
I am just playing with jison and trying to create very simple parser
(both JS and PHP) for parsing logic expressions like:

A AND (B OR C)

The JS part works great, but I am fighting with the PHP part now. It
seems it assign incorrect value to the yytext variable. Is it known
issue? Or am I doing something wrong?

Attaching my .jison file. For expression like: A AND B AND (C OR D)
I would expect the values of yytext variable in the last rule would be
A, B, C, or D. But instead of the values are: AND, OR, )

thanks fro your help
Karel

Re: [jison] yytext in PHP jison

From:
Robert Plummer
Date:
2012-05-04 @ 13:49
Can you send me the output and the text you are parsing?

On Tue, Apr 17, 2012 at 12:33 PM, Karel Kozlik <karel@iptel.org> wrote:

>  Hello guys,
> I am just playing with jison and trying to create very simple parser
> (both JS and PHP) for parsing logic expressions like:
>
> A AND (B OR C)
>
> The JS part works great, but I am fighting with the PHP part now. It
> seems it assign incorrect value to the yytext variable. Is it known
> issue? Or am I doing something wrong?
>
> Attaching my .jison file. For expression like: A AND B AND (C OR D)
> I would expect the values of yytext variable in the last rule would be
> A, B, C, or D. But instead of the values are: AND, OR, )
>
> thanks fro your help
> Karel
>
>
>
> /* description: Parses grouping expressions. */
>
> //phpOption parserClass:Grouping_expression_parser
> //phpOption lexerClass:Grouping_expression_lexer
>
> /* lexical grammar */
> %lex
>
> %%
>
> \s+                   /* skip whitespace */
> [A-Za-z]\b            return 'TERM'
> [aA][nN][dD]\b        return 'AND'
> [oO][rR]\b            return 'OR'
> "("                   return '('
> ")"                   return ')'
> <<EOF>>               return 'EOF'
>
>
> /lex
>
> /* operator associations and precedence */
> %left 'OR'
> %left 'AND'
>
> %start expressions
>
>
> %% /* language grammar */
>
> expressions
>    : e EOF
>        {{
>            return $1;
>        }}
>    ;
>
> e
>    : e 'OR' e
>        {{
>            $$ = {term: false, op: "OR", l_val: $1, r_val: $3};     //js
> //php       $$ = (object)array("term" => false, "op" => "OR", "l_val" =>
> $1, "r_val" => $3);
>        }}
>    | e 'AND' e
>        {{
>            $$ = {term: false, op: "AND", l_val: $1, r_val: $3};    //js
> //php       $$ = (object)array("term" => false, "op" => "AND", "l_val" =>
> $1, "r_val" => $3);
>        }}
>    | '(' e ')'
>        {{
>            $$ = $2;
>        }}
>    | TERM
>        {{
>            $$ = {term: true, label: yytext.toUpperCase(), fast_search:
> yy[yytext.toUpperCase()]};  //js
> //php       $$ = (object)array("term" => true, "label" =>
> strtoupper($this->yytext), "fast_search" =>
> $this->yy[strtoupper($this->yytext)]);
>        }}
>    ;
>
>
>
>
>


-- 
Robert Plummer

Re: [jison] yytext in PHP jison

From:
Karel Kozlik
Date:
2012-05-04 @ 15:23
Thanks for your reply Robert. I fixed the issue in the meantime by 
replacing yytext with $1.

I am new to jison, so I even don't know whether use of yytext is correct 
in this case. And neither what's the difference between yytext and $1. 
But yytext is used in the calculator demo on jison homepage, so I 
started with it.

But if you still want to check whether it is an issue in PHP jison I am 
sending my generated output in the attachment. The text I am parsing is: 
"A AND B AND (C OR D)"

thanks,
Karel

Dne 4.5.2012 15:49, Robert Plummer napsal(a):
> Can you send me the output and the text you are parsing?
>
> On Tue, Apr 17, 2012 at 12:33 PM, Karel Kozlik <karel@iptel.org
> <mailto:karel@iptel.org>> wrote:
>
>       Hello guys,
>     I am just playing with jison and trying to create very simple parser
>     (both JS and PHP) for parsing logic expressions like:
>
>     A AND (B OR C)
>
>     The JS part works great, but I am fighting with the PHP part now. It
>     seems it assign incorrect value to the yytext variable. Is it known
>     issue? Or am I doing something wrong?
>
>     Attaching my .jison file. For expression like: A AND B AND (C OR D)
>     I would expect the values of yytext variable in the last rule would be
>     A, B, C, or D. But instead of the values are: AND, OR, )
>
>     thanks fro your help
>     Karel
>
>
>
>     /* description: Parses grouping expressions. */
>
>     //phpOption parserClass:Grouping_expression_parser
>     //phpOption lexerClass:Grouping_expression_lexer
>
>     /* lexical grammar */
>     %lex
>
>     %%
>
>     \s+                   /* skip whitespace */
>     [A-Za-z]\b            return 'TERM'
>     [aA][nN][dD]\b        return 'AND'
>     [oO][rR]\b            return 'OR'
>     "("                   return '('
>     ")"                   return ')'
>     <<EOF>>               return 'EOF'
>
>
>     /lex
>
>     /* operator associations and precedence */
>     %left 'OR'
>     %left 'AND'
>
>     %start expressions
>
>
>     %% /* language grammar */
>
>     expressions
>         : e EOF
>             {{
>                 return $1;
>             }}
>         ;
>
>     e
>         : e 'OR' e
>             {{
>                 $$ = {term: false, op: "OR", l_val: $1, r_val: $3};     //js
>     //php       $$ = (object)array("term" => false, "op" => "OR",
>     "l_val" => $1, "r_val" => $3);
>             }}
>         | e 'AND' e
>             {{
>                 $$ = {term: false, op: "AND", l_val: $1, r_val: $3};    //js
>     //php       $$ = (object)array("term" => false, "op" => "AND",
>     "l_val" => $1, "r_val" => $3);
>             }}
>         | '(' e ')'
>             {{
>                 $$ = $2;
>             }}
>         | TERM
>             {{
>                 $$ = {term: true, label: yytext.toUpperCase(),
>     fast_search: yy[yytext.toUpperCase()]};  //js
>     //php       $$ = (object)array("term" => true, "label" =>
>     strtoupper($this->yytext), "fast_search" =>
>     $this->yy[strtoupper($this->yytext)]);
>             }}
>         ;
>
>
>
>
>
>
>
> --
> Robert Plummer