librelist archives

« back to archive

[jison] Syntax question

[jison] Syntax question

From:
Terry Riegel
Date:
2010-07-29 @ 20:26
Trying to get my head around this a bit at a time. I modified the 
calculator like so...


I added right before <<EOF>>, this...
==========================
"copy"       {return 'copy';}
"/copy"      {return '/copy';}
==========================





then I added this to the expressions....
==========================
   | 'copy' e '/copy'
     {$$ = $2;}
==========================





Generate parser seemed to be ok with my additions. 
I expected this statement to return 10...
==========================
copy 5*2 /copy
==========================





Instead I get a syntax error...
==========================
Parse error on line 1:
copy 5*2 /copy
--------------^
Expecting '-', '(', 'NUMBER', 'E', 'PI', 'copy'
==========================



What am I missing conceptually, syntactically?

Thanks,

Terry Riegel

Re: [jison] Syntax question

From:
Zachary Carter
Date:
2010-07-29 @ 20:50
Hey Terry,


On Thu, Jul 29, 2010 at 4:26 PM, Terry Riegel <triegel@positiveaction.org>wrote:

> Trying to get my head around this a bit at a time. I modified the
> calculator like so...
>
>
> I added right before <<EOF>>, this...
> ==========================
> "copy"       {retur n 'copy';}
> "/copy"      {return '/copy';}
> ==========================
>

The division symbol "/" is listed before "/copy", so "/copy" is tokenized as
"/" and "copy" instead of your expected token "/copy". Simply list "/copy"
before the division symbol to have it match correctly.


>
>
>
>
>
>
> then I added this to the expressions....
> ==========================
>    | 'copy' e '/copy'
>      {$$ = $2;}
> ==========================
>
>
>
>
>
> Generate parser seemed to be ok with my additions.
> I expected this statement to return 10...
> ==========================
> copy 5*2 /copy
> ==========================
>
>
>
>
>
> Instead I get a syntax error...
> ==========================
> Parse error on line 1:
> copy 5*2 /copy
> --------------^
> Expecting '-', '(', 'NUMBER', 'E', 'PI', 'copy'
> ==========================
>
>
>
> W hat am I missing conceptually, syntactically?
>

Everything else looks correct.


>
>
> Thanks,
>
> Terry Riegel
>



-- 
Zach Carter

Re: [jison] Syntax question

From:
Terry Riegel
Date:
2010-07-30 @ 13:13
Very cool, thanks for the quick reply. I would love to learn some of the 
nitty gritty of this kind of stuff, do you recommend anything that would 
start slowly and build on concepts. I especially like your calculator 
example on your site but it leaves me asking all sorts of questions about 
how it works and how one could slowly build a language with it.

I have two hopefully simple questions.

1) I was trying to figure out how to make the calculator work with an 
equal sign, so I added this...
==========================
    | e '=' e
       { if($1 = $3) {$$=true;} else {$$=false;} }
==========================



This didn't make it past the generate parser stage...
==========================
Oops. Make sure your grammar is in the correct format.
Error: Parse error on line 63:
...$1 = $3) {$$=true;} else {$$=false;} } 
-----------------------^
Expecting ';', '|'
==========================


2) How would I implement a simple if then construct, something like...

if a=b then
 statement
else
 statement
/if

Do I need to define statement first? How would I do that.

I like the visual charts found on json.org, I am just trying to figure out
how those can be transformed to the jison format.


For example, this is one from the site defining an object...