librelist archives

« back to archive

Start Condition Problem

Start Condition Problem

From:
phil jones
Date:
2013-04-28 @ 03:51
Hi,

I'm having a problem getting start conditions to work. I'm trying to add an
option to a language that lets people add free text labels to what their
doing on lines starting with __.

I found that just adding a rule to pick up free text was conflicting with
matching everything else in the language so I added the following two lines
to the lexer :

<text>.*\n               %{ this.popState(); return "TEXT"; %}
"__"                     %{ this.begin("text"); return "__"; %}

I assume that __ now puts the lexer into a text mode, which then allows the
preceding rule to be applied.

However jison blows up on this with the following errors :

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
TypeError: Cannot read property 'rules' of undefined
    at prepareRules

(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:42:47)
    at Object.buildActions

(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:112:18)
    at Object.RegExpLexer

(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:131:39)
    at [object Object].Jison_Generator
(/home/phil/local/node/lib/node_modules/jison/lib/jison.js:108:22)
    at [object Object].<anonymous>
(/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:23:28)
    at new <anonymous>
(/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:77:70)
    at new Jison_Generator
(/home/phil/local/node/lib/node_modules/jison/lib/jison.js:1578:20)
    at processGrammar
(/home/phil/local/node/lib/node_modules/jison/lib/cli.js:87:21)
    at Object.main
(/home/phil/local/node/lib/node_modules/jison/lib/cli.js:61:56)
    at Object.<anonymous>
(/home/phil/local/node/lib/node_modules/jison/lib/cli.js:105:13)

Anyone have an idea what I'm doing wrong?

cheers

Phil

Re: [jison] Start Condition Problem

From:
Robert Plummer
Date:
2013-04-28 @ 16:43
Have you defined your condition?
/* lexical grammar */
%lex
/* lexical states */
$s text //<- I believe this may be missing
/*begin lexing */
%%

On Sat, Apr 27, 2013 at 11:51 PM, phil jones <interstar@gmail.com> wrote:

> Hi,
>
> I'm having a problem getting start conditions to work. I'm trying to add
> an option to a language that lets people add free text labels to what their
> doing on lines starting with __.
>
> I found that just adding a rule to pick up free text was conflicting with
> matching everything else in the language so I added the following two lines
> to the lexer :
>
> <text>.*\n               %{ this.popState(); return "TEXT"; %}
> "__"                     %{ this.begin("text"); return "__"; %}
>
> I assume that __ now puts the lexer into a text mode, which then allows
> the preceding rule to be applied.
>
> However jison blows up on this with the following errors :
>
> node.js:201
>         throw e; // process.nextTick error, or 'error' event on first tick
>               ^
> TypeError: Cannot read property 'rules' of undefined
>     at prepareRules
> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:42:47)
>     at Object.buildActions
> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:112:18)
>     at Object.RegExpLexer
> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:131:39)
>     at [object Object].Jison_Generator
> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:108:22)
>     at [object Object].<anonymous>
> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:23:28)
>     at new <anonymous>
> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:77:70)
>     at new Jison_Generator
> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:1578:20)
>     at processGrammar
> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:87:21)
>     at Object.main
> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:61:56)
>     at Object.<anonymous>
> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:105:13)
>
> Anyone have an idea what I'm doing wrong?
>
> cheers
>
> Phil
>



-- 
Robert Plummer

Re: [jison] Start Condition Problem

From:
Robert Plummer
Date:
2013-04-28 @ 16:44
Would you mind posting your whole .jison file?

On Sun, Apr 28, 2013 at 12:43 PM, Robert Plummer <
robertleeplummerjr@gmail.com> wrote:

> Have you defined your condition?
> /* lexical grammar */
> %lex
> /* lexical states */
> $s text //<- I believe this may be missing
> /*begin lexing */
> %%
>
> On Sat, Apr 27, 2013 at 11:51 PM, phil jones <interstar@gmail.com> wrote:
>
>> Hi,
>>
>> I'm having a problem getting start conditions to work. I'm trying to add
>> an option to a language that lets people add free text labels to what their
>> doing on lines starting with __.
>>
>> I found that just adding a rule to pick up free text was conflicting with
>> matching everything else in the language so I added the following two lines
>> to the lexer :
>>
>> <text>.*\n               %{ this.popState(); return "TEXT"; %}
>> "__"                     %{ this.begin("text"); return "__"; %}
>>
>> I assume that __ now puts the lexer into a text mode, which then allows
>> the preceding rule to be applied.
>>
>> However jison blows up on this with the following errors :
>>
>> node.js:201
>>         throw e; // process.nextTick error, or 'error' event on first tick
>>               ^
>> TypeError: Cannot read property 'rules' of undefined
>>     at prepareRules
>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:42:47)
>>     at Object.buildActions
>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:112:18)
>>     at Object.RegExpLexer
>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:131:39)
>>     at [object Object].Jison_Generator
>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:108:22)
>>     at [object Object].<anonymous>
>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:23:28)
>>     at new <anonymous>
>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:77:70)
>>     at new Jison_Generator
>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:1578:20)
>>     at processGrammar
>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:87:21)
>>     at Object.main
>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:61:56)
>>     at Object.<anonymous>
>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:105:13)
>>
>> Anyone have an idea what I'm doing wrong?
>>
>> cheers
>>
>> Phil
>>
>
>
>
> --
> Robert Plummer
>



-- 
Robert Plummer

Re: [jison] Start Condition Problem

From:
phil jones
Date:
2013-04-29 @ 02:39
Thanks Robert,

yes, I'd missed that condition declaration. I added it and the jison file
now compiles.

However, now when I try to run the parser on a test file which contains the
line
__ This is a test

 I get the following :

Error: Parse error on line 3:
... = area * height__ This is a test
----------------------^
Expecting 'TEXT', got 'VARNAME'

Obviously what I'm asking the lexer to do still isn't quite right.

My parser is basically a variant of the calculator example that outputs an
abstract syntax tree rather than evaluating the expression directly. I want
to intersperse the calculations with explanatory text, hence I'm adding
these "text" lines.

/* description: Parses end executes mathematical expressions. */

/* lexical grammar */
%lex

%s text

%%

\s+                      /* skip whitespace */
<text>.*\n               %{ this.popState(); return "TEXT"; %}
"__"                     %{ this.begin("text"); return "__"; %}

[0-9]+("."[0-9]+)?\b     return 'NUMBER'
[A-Za-z][A-Za-z0-9_]*\b  return 'VARNAME'
"*"                      return '*'
"/"                      return '/'
"-"                      return '-'
"+"                      return '+'
"^"                      return '^'
"!"                      return '!'
"%"                      return '%'
"("                      return '('
")"                      return ')'
"="                      return '='
">>"                     return ">>"
<<EOF>>                  return 'EOF'
.                        return 'INVALID'

/lex

/* operator associations and precedence */

%left '+' '-'
%left '*' '/'
%left '^'
%right '!'
%right '%'
%left UMINUS

%start prog

%% /* language grammar */

prog
    : statements EOF
       {
          var result = '{"PROG":['+$1+']}';
          typeof console !== 'undefined' ? console.log(result) :
print(result);
          return result;
       }
    ;

statements
    : statement
        { $$ = $1; }
    | statement statements
        { $$ = $1 + "," + $2; }
    ;

statement
    : assign
        { $$ = $1; }
    | text
        { $$ = $1; }
    ;

text
    : '__' TEXT
        { $$ = '{"tag":"TEXT", "value":"' + $1 + '"}' }
    ;

assign
    : v '=' e
        { $$ = '{"tag":"ASSIGN","left":'+$1+',"right":'+$3+'}' }
    ;


v
    : VARNAME
        {$$ = '{"tag":"VARNAME","name":"' + $1 + '"}'}
    ;

e
    : e '+' e
        {$$ = '{"tag":"+", "left":'+$1+', "right":'+$3+'}' }
    | e '-' e
        {$$ = '{"tag":"-", "left":'+$1+', "right":'+$3+'}' }
    | e '*' e
        {$$ = '{"tag":"*", "left":'+$1+', "right":'+$3+'}' }
    | e '/' e
        {$$ = '{"tag":"/", "left":'+$1+', "right":'+$3+'}'}
    | e '^' e
        {$$ = '{"tag":"EXP", "left":'+$1+', "right":'+$3+'}' }
    | e '!'
        {$$ = '{"tag":"FACTORIAL", "left":'+$1+'}' }
    | e '%'
        {$$ = '{"tag":"MODULO", "left":'+$1+', "right":'+$3+'}'}
    | '-' e %prec UMINUS
        {$$ = '{"tag":"NEGATE", "left":'+$2 + '}' }
    | '(' e ')'
        {$$ = $2;}
    | NUMBER
        {$$ = '{"tag":"CONSTANT","value":'+Number(yytext)+'}';}
    | v
        {$$ = $1;}
    ;


cheers

Phil



On 28 April 2013 13:44, Robert Plummer <robertleeplummerjr@gmail.com> wrote:

> Would you mind posting your whole .jison file?
>
>
> On Sun, Apr 28, 2013 at 12:43 PM, Robert Plummer <
> robertleeplummerjr@gmail.com> wrote:
>
>> Have you defined your condition?
>> /* lexical grammar */
>> %lex
>> /* lexical states */
>> $s text //<- I believe this may be missing
>> /*begin lexing */
>> %%
>>
>> On Sat, Apr 27, 2013 at 11:51 PM, phil jones <interstar@gmail.com> wrote:
>>
>>> Hi,
>>>
>>> I'm having a problem getting start conditions to work. I'm trying to add
>>> an option to a language that lets people add free text labels to what their
>>> doing on lines starting with __.
>>>
>>> I found that just adding a rule to pick up free text was conflicting
>>> with matching everything else in the language so I added the following two
>>> lines to the lexer :
>>>
>>> <text>.*\n               %{ this.popState(); return "TEXT"; %}
>>> "__"                     %{ this.begin("text"); return "__"; %}
>>>
>>> I assume that __ now puts the lexer into a text mode, which then allows
>>> the preceding rule to be applied.
>>>
>>> However jison blows up on this with the following errors :
>>>
>>> node.js:201
>>>         throw e; // process.nextTick error, or 'error' event on first
>>> tick
>>>               ^
>>> TypeError: Cannot read property 'rules' of undefined
>>>     at prepareRules
>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:42:47)
>>>     at Object.buildActions
>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:112:18)
>>>     at Object.RegExpLexer
>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:131:39)
>>>     at [object Object].Jison_Generator
>>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:108:22)
>>>     at [object Object].<anonymous>
>>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:23:28)
>>>     at new <anonymous>
>>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:77:70)
>>>     at new Jison_Generator
>>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:1578:20)
>>>     at processGrammar
>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:87:21)
>>>     at Object.main
>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:61:56)
>>>     at Object.<anonymous>
>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:105:13)
>>>
>>> Anyone have an idea what I'm doing wrong?
>>>
>>> cheers
>>>
>>> Phil
>>>
>>
>>
>>
>> --
>> Robert Plummer
>>
>
>
>
> --
> Robert Plummer

Re: [jison] Start Condition Problem

From:
Robert Plummer
Date:
2013-04-29 @ 09:18
Make the \n optional by putting a '?' after it.
On Apr 28, 2013 10:39 PM, "phil jones" <interstar@gmail.com> wrote:

> Thanks Robert,
>
> yes, I'd missed that condition declaration. I added it and the jison file
> now compiles.
>
> However, now when I try to run the parser on a test file which contains
> the line
> __ This is a test
>
>  I get the following :
>
> Error: Parse error on line 3:
> ... = area * height__ This is a test
> ----------------------^
> Expecting 'TEXT', got 'VARNAME'
>
> Obviously what I'm asking the lexer to do still isn't quite right.
>
> My parser is basically a variant of the calculator example that outputs an
> abstract syntax tree rather than evaluating the expression directly. I want
> to intersperse the calculations with explanatory text, hence I'm adding
> these "text" lines.
>
> /* description: Parses end executes mathematical expressions. */
>
> /* lexical grammar */
> %lex
>
> %s text
>
> %%
>
> \s+                      /* skip whitespace */
> <text>.*\n               %{ this.popState(); return "TEXT"; %}
> "__"                     %{ this.begin("text"); return "__"; %}
>
> [0-9]+("."[0-9]+)?\b     return 'NUMBER'
> [A-Za-z][A-Za-z0-9_]*\b  return 'VARNAME'
> "*"                      return '*'
> "/"                      return '/'
> "-"                      return '-'
> "+"                      return '+'
> "^"                      return '^'
> "!"                      return '!'
> "%"                      return '%'
> "("                      return '('
> ")"                      return ')'
> "="                      return '='
> ">>"                     return ">>"
> <<EOF>>                  return 'EOF'
> .                        return 'INVALID'
>
> /lex
>
> /* operator associations and precedence */
>
> %left '+' '-'
> %left '*' '/'
> %left '^'
> %right '!'
> %right '%'
> %left UMINUS
>
> %start prog
>
> %% /* language grammar */
>
> prog
>     : statements EOF
>        {
>           var result = '{"PROG":['+$1+']}';
>           typeof console !== 'undefined' ? console.log(result) :
> print(result);
>           return result;
>        }
>     ;
>
> statements
>     : statement
>         { $$ = $1; }
>     | statement statements
>         { $$ = $1 + "," + $2; }
>     ;
>
> statement
>     : assign
>         { $$ = $1; }
>     | text
>         { $$ = $1; }
>     ;
>
> text
>     : '__' TEXT
>         { $$ = '{"tag":"TEXT", "value":"' + $1 + '"}' }
>     ;
>
> assign
>     : v '=' e
>         { $$ = '{"tag":"ASSIGN","left":'+$1+',"right":'+$3+'}' }
>      ;
>
>
> v
>     : VARNAME
>         {$$ = '{"tag":"VARNAME","name":"' + $1 + '"}'}
>     ;
>
> e
>     : e '+' e
>         {$$ = '{"tag":"+", "left":'+$1+', "right":'+$3+'}' }
>     | e '-' e
>         {$$ = '{"tag":"-", "left":'+$1+', "right":'+$3+'}' }
>     | e '*' e
>         {$$ = '{"tag":"*", "left":'+$1+', "right":'+$3+'}' }
>     | e '/' e
>         {$$ = '{"tag":"/", "left":'+$1+', "right":'+$3+'}'}
>     | e '^' e
>         {$$ = '{"tag":"EXP", "left":'+$1+', "right":'+$3+'}' }
>     | e '!'
>         {$$ = '{"tag":"FACTORIAL", "left":'+$1+'}' }
>     | e '%'
>         {$$ = '{"tag":"MODULO", "left":'+$1+', "right":'+$3+'}'}
>     | '-' e %prec UMINUS
>         {$$ = '{"tag":"NEGATE", "left":'+$2 + '}' }
>     | '(' e ')'
>         {$$ = $2;}
>     | NUMBER
>         {$$ = '{"tag":"CONSTANT","value":'+Number(yytext)+'}';}
>     | v
>         {$$ = $1;}
>     ;
>
>
> cheers
>
> Phil
>
>
>
> On 28 April 2013 13:44, Robert Plummer <robertleeplummerjr@gmail.com>wrote:
>
>> Would you mind posting your whole .jison file?
>>
>>
>> On Sun, Apr 28, 2013 at 12:43 PM, Robert Plummer <
>> robertleeplummerjr@gmail.com> wrote:
>>
>>> Have you defined your condition?
>>> /* lexical grammar */
>>> %lex
>>> /* lexical states */
>>> $s text //<- I believe this may be missing
>>> /*begin lexing */
>>> %%
>>>
>>> On Sat, Apr 27, 2013 at 11:51 PM, phil jones <interstar@gmail.com>wrote:
>>>
>>>> Hi,
>>>>
>>>> I'm having a problem getting start conditions to work. I'm trying to
>>>> add an option to a language that lets people add free text labels to what
>>>> their doing on lines starting with __.
>>>>
>>>> I found that just adding a rule to pick up free text was conflicting
>>>> with matching everything else in the language so I added the following two
>>>> lines to the lexer :
>>>>
>>>> <text>.*\n               %{ this.popState(); return "TEXT"; %}
>>>> "__"                     %{ this.begin("text"); return "__"; %}
>>>>
>>>> I assume that __ now puts the lexer into a text mode, which then allows
>>>> the preceding rule to be applied.
>>>>
>>>> However jison blows up on this with the following errors :
>>>>
>>>> node.js:201
>>>>         throw e; // process.nextTick error, or 'error' event on first
>>>> tick
>>>>               ^
>>>> TypeError: Cannot read property 'rules' of undefined
>>>>     at prepareRules
>>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:42:47)
>>>>     at Object.buildActions
>>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:112:18)
>>>>     at Object.RegExpLexer
>>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:131:39)
>>>>     at [object Object].Jison_Generator
>>>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:108:22)
>>>>     at [object Object].<anonymous>
>>>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:23:28)
>>>>     at new <anonymous>
>>>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:77:70)
>>>>     at new Jison_Generator
>>>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:1578:20)
>>>>     at processGrammar
>>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:87:21)
>>>>     at Object.main
>>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:61:56)
>>>>     at Object.<anonymous>
>>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:105:13)
>>>>
>>>> Anyone have an idea what I'm doing wrong?
>>>>
>>>> cheers
>>>>
>>>> Phil
>>>>
>>>
>>>
>>>
>>> --
>>> Robert Plummer
>>>
>>
>>
>>
>> --
>> Robert Plummer
>
>
>

Re: [jison] Start Condition Problem

From:
phil jones
Date:
2013-04-29 @ 14:43
Doh!

Yes. :-)

Many thanks Robert Plummer.

Phil

On 29 April 2013 06:18, Robert Plummer <robertleeplummerjr@gmail.com> wrote:

> Make the \n optional by putting a '?' after it.
> On Apr 28, 2013 10:39 PM, "phil jones" <interstar@gmail.com> wrote:
>
>> Thanks Robert,
>>
>> yes, I'd missed that condition declaration. I added it and the jison file
>> now compiles.
>>
>> However, now when I try to run the parser on a test file which contains
>> the line
>> __ This is a test
>>
>>  I get the following :
>>
>> Error: Parse error on line 3:
>> ... = area * height__ This is a test
>> ----------------------^
>> Expecting 'TEXT', got 'VARNAME'
>>
>> Obviously what I'm asking the lexer to do still isn't quite right.
>>
>> My parser is basically a variant of the calculator example that outputs
>> an abstract syntax tree rather than evaluating the expression directly. I
>> want to intersperse the calculations with explanatory text, hence I'm
>> adding these "text" lines.
>>
>> /* description: Parses end executes mathematical expressions. */
>>
>> /* lexical grammar */
>> %lex
>>
>> %s text
>>
>> %%
>>
>> \s+                      /* skip whitespace */
>> <text>.*\n               %{ this.popState(); return "TEXT"; %}
>> "__"                     %{ this.begin("text"); return "__"; %}
>>
>> [0-9]+("."[0-9]+)?\b     return 'NUMBER'
>> [A-Za-z][A-Za-z0-9_]*\b  return 'VARNAME'
>> "*"                      return '*'
>> "/"                      return '/'
>> "-"                      return '-'
>> "+"                      return '+'
>> "^"                      return '^'
>> "!"                      return '!'
>> "%"                      return '%'
>> "("                      return '('
>> ")"                      return ')'
>> "="                      return '='
>> ">>"                     return ">>"
>> <<EOF>>                  return 'EOF'
>> .                        return 'INVALID'
>>
>> /lex
>>
>> /* operator associations and precedence */
>>
>> %left '+' '-'
>> %left '*' '/'
>> %left '^'
>> %right '!'
>> %right '%'
>> %left UMINUS
>>
>> %start prog
>>
>> %% /* language grammar */
>>
>> prog
>>     : statements EOF
>>        {
>>           var result = '{"PROG":['+$1+']}';
>>           typeof console !== 'undefined' ? console.log(result) :
>> print(result);
>>           return result;
>>        }
>>     ;
>>
>> statements
>>     : statement
>>         { $$ = $1; }
>>     | statement statements
>>         { $$ = $1 + "," + $2; }
>>     ;
>>
>> statement
>>     : assign
>>         { $$ = $1; }
>>     | text
>>         { $$ = $1; }
>>     ;
>>
>> text
>>     : '__' TEXT
>>         { $$ = '{"tag":"TEXT", "value":"' + $1 + '"}' }
>>     ;
>>
>> assign
>>     : v '=' e
>>         { $$ = '{"tag":"ASSIGN","left":'+$1+',"right":'+$3+'}' }
>>      ;
>>
>>
>> v
>>     : VARNAME
>>         {$$ = '{"tag":"VARNAME","name":"' + $1 + '"}'}
>>     ;
>>
>> e
>>     : e '+' e
>>         {$$ = '{"tag":"+", "left":'+$1+', "right":'+$3+'}' }
>>     | e '-' e
>>         {$$ = '{"tag":"-", "left":'+$1+', "right":'+$3+'}' }
>>     | e '*' e
>>         {$$ = '{"tag":"*", "left":'+$1+', "right":'+$3+'}' }
>>     | e '/' e
>>         {$$ = '{"tag":"/", "left":'+$1+', "right":'+$3+'}'}
>>     | e '^' e
>>         {$$ = '{"tag":"EXP", "left":'+$1+', "right":'+$3+'}' }
>>     | e '!'
>>         {$$ = '{"tag":"FACTORIAL", "left":'+$1+'}' }
>>     | e '%'
>>         {$$ = '{"tag":"MODULO", "left":'+$1+', "right":'+$3+'}'}
>>     | '-' e %prec UMINUS
>>         {$$ = '{"tag":"NEGATE", "left":'+$2 + '}' }
>>     | '(' e ')'
>>         {$$ = $2;}
>>     | NUMBER
>>         {$$ = '{"tag":"CONSTANT","value":'+Number(yytext)+'}';}
>>     | v
>>         {$$ = $1;}
>>     ;
>>
>>
>> cheers
>>
>> Phil
>>
>>
>>
>> On 28 April 2013 13:44, Robert Plummer <robertleeplummerjr@gmail.com>wrote:
>>
>>> Would you mind posting your whole .jison file?
>>>
>>>
>>> On Sun, Apr 28, 2013 at 12:43 PM, Robert Plummer <
>>> robertleeplummerjr@gmail.com> wrote:
>>>
>>>> Have you defined your condition?
>>>> /* lexical grammar */
>>>> %lex
>>>> /* lexical states */
>>>> $s text //<- I believe this may be missing
>>>> /*begin lexing */
>>>> %%
>>>>
>>>> On Sat, Apr 27, 2013 at 11:51 PM, phil jones <interstar@gmail.com>wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I'm having a problem getting start conditions to work. I'm trying to
>>>>> add an option to a language that lets people add free text labels to what
>>>>> their doing on lines starting with __.
>>>>>
>>>>> I found that just adding a rule to pick up free text was conflicting
>>>>> with matching everything else in the language so I added the following two
>>>>> lines to the lexer :
>>>>>
>>>>> <text>.*\n               %{ this.popState(); return "TEXT"; %}
>>>>> "__"                     %{ this.begin("text"); return "__"; %}
>>>>>
>>>>> I assume that __ now puts the lexer into a text mode, which then
>>>>> allows the preceding rule to be applied.
>>>>>
>>>>> However jison blows up on this with the following errors :
>>>>>
>>>>> node.js:201
>>>>>         throw e; // process.nextTick error, or 'error' event on first
>>>>> tick
>>>>>               ^
>>>>> TypeError: Cannot read property 'rules' of undefined
>>>>>     at prepareRules
>>>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:42:47)
>>>>>     at Object.buildActions
>>>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:112:18)
>>>>>     at Object.RegExpLexer
>>>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:131:39)
>>>>>     at [object Object].Jison_Generator
>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:108:22)
>>>>>     at [object Object].<anonymous>
>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:23:28)
>>>>>     at new <anonymous>
>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:77:70)
>>>>>     at new Jison_Generator
>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:1578:20)
>>>>>     at processGrammar
>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:87:21)
>>>>>     at Object.main
>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:61:56)
>>>>>     at Object.<anonymous>
>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:105:13)
>>>>>
>>>>> Anyone have an idea what I'm doing wrong?
>>>>>
>>>>> cheers
>>>>>
>>>>> Phil
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Robert Plummer
>>>>
>>>
>>>
>>>
>>> --
>>> Robert Plummer
>>
>>
>>

Re: [jison] Start Condition Problem

From:
Robert Plummer
Date:
2013-04-29 @ 15:05
It is what I do.

On Mon, Apr 29, 2013 at 10:43 AM, phil jones <interstar@gmail.com> wrote:

> Doh!
>
> Yes. :-)
>
> Many thanks Robert Plummer.
>
> Phil
>
>
> On 29 April 2013 06:18, Robert Plummer <robertleeplummerjr@gmail.com>wrote:
>
>> Make the \n optional by putting a '?' after it.
>> On Apr 28, 2013 10:39 PM, "phil jones" <interstar@gmail.com> wrote:
>>
>>> Thanks Robert,
>>>
>>> yes, I'd missed that condition declaration. I added it and the jison
>>> file now compiles.
>>>
>>> However, now when I try to run the parser on a test file which contains
>>> the line
>>> __ This is a test
>>>
>>>  I get the following :
>>>
>>> Error: Parse error on line 3:
>>> ... = area * height__ This is a test
>>> ----------------------^
>>> Expecting 'TEXT', got 'VARNAME'
>>>
>>> Obviously what I'm asking the lexer to do still isn't quite right.
>>>
>>> My parser is basically a variant of the calculator example that outputs
>>> an abstract syntax tree rather than evaluating the expression directly. I
>>> want to intersperse the calculations with explanatory text, hence I'm
>>> adding these "text" lines.
>>>
>>> /* description: Parses end executes mathematical expressions. */
>>>
>>> /* lexical grammar */
>>> %lex
>>>
>>> %s text
>>>
>>> %%
>>>
>>> \s+                      /* skip whitespace */
>>> <text>.*\n               %{ this.popState(); return "TEXT"; %}
>>> "__"                     %{ this.begin("text"); return "__"; %}
>>>
>>> [0-9]+("."[0-9]+)?\b     return 'NUMBER'
>>> [A-Za-z][A-Za-z0-9_]*\b  return 'VARNAME'
>>> "*"                      return '*'
>>> "/"                      return '/'
>>> "-"                      return '-'
>>> "+"                      return '+'
>>> "^"                      return '^'
>>> "!"                      return '!'
>>> "%"                      return '%'
>>> "("                      return '('
>>> ")"                      return ')'
>>> "="                      return '='
>>> ">>"                     return ">>"
>>> <<EOF>>                  return 'EOF'
>>> .                        return 'INVALID'
>>>
>>> /lex
>>>
>>> /* operator associations and precedence */
>>>
>>> %left '+' '-'
>>> %left '*' '/'
>>> %left '^'
>>> %right '!'
>>> %right '%'
>>> %left UMINUS
>>>
>>> %start prog
>>>
>>> %% /* language grammar */
>>>
>>> prog
>>>     : statements EOF
>>>        {
>>>           var result = '{"PROG":['+$1+']}';
>>>           typeof console !== 'undefined' ? console.log(result) :
>>> print(result);
>>>           return result;
>>>        }
>>>     ;
>>>
>>> statements
>>>     : statement
>>>         { $$ = $1; }
>>>     | statement statements
>>>         { $$ = $1 + "," + $2; }
>>>     ;
>>>
>>> statement
>>>     : assign
>>>         { $$ = $1; }
>>>     | text
>>>         { $$ = $1; }
>>>     ;
>>>
>>> text
>>>     : '__' TEXT
>>>         { $$ = '{"tag":"TEXT", "value":"' + $1 + '"}' }
>>>     ;
>>>
>>> assign
>>>     : v '=' e
>>>         { $$ = '{"tag":"ASSIGN","left":'+$1+',"right":'+$3+'}' }
>>>      ;
>>>
>>>
>>> v
>>>     : VARNAME
>>>         {$$ = '{"tag":"VARNAME","name":"' + $1 + '"}'}
>>>     ;
>>>
>>> e
>>>     : e '+' e
>>>         {$$ = '{"tag":"+", "left":'+$1+', "right":'+$3+'}' }
>>>     | e '-' e
>>>         {$$ = '{"tag":"-", "left":'+$1+', "right":'+$3+'}' }
>>>     | e '*' e
>>>         {$$ = '{"tag":"*", "left":'+$1+', "right":'+$3+'}' }
>>>     | e '/' e
>>>         {$$ = '{"tag":"/", "left":'+$1+', "right":'+$3+'}'}
>>>     | e '^' e
>>>         {$$ = '{"tag":"EXP", "left":'+$1+', "right":'+$3+'}' }
>>>     | e '!'
>>>         {$$ = '{"tag":"FACTORIAL", "left":'+$1+'}' }
>>>     | e '%'
>>>         {$$ = '{"tag":"MODULO", "left":'+$1+', "right":'+$3+'}'}
>>>     | '-' e %prec UMINUS
>>>         {$$ = '{"tag":"NEGATE", "left":'+$2 + '}' }
>>>     | '(' e ')'
>>>         {$$ = $2;}
>>>     | NUMBER
>>>         {$$ = '{"tag":"CONSTANT","value":'+Number(yytext)+'}';}
>>>     | v
>>>         {$$ = $1;}
>>>     ;
>>>
>>>
>>> cheers
>>>
>>> Phil
>>>
>>>
>>>
>>> On 28 April 2013 13:44, Robert Plummer <robertleeplummerjr@gmail.com>wrote:
>>>
>>>> Would you mind posting your whole .jison file?
>>>>
>>>>
>>>> On Sun, Apr 28, 2013 at 12:43 PM, Robert Plummer <
>>>> robertleeplummerjr@gmail.com> wrote:
>>>>
>>>>> Have you defined your condition?
>>>>> /* lexical grammar */
>>>>> %lex
>>>>> /* lexical states */
>>>>> $s text //<- I believe this may be missing
>>>>> /*begin lexing */
>>>>> %%
>>>>>
>>>>> On Sat, Apr 27, 2013 at 11:51 PM, phil jones <interstar@gmail.com>wrote:
>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I'm having a problem getting start conditions to work. I'm trying to
>>>>>> add an option to a language that lets people add free text labels to what
>>>>>> their doing on lines starting with __.
>>>>>>
>>>>>> I found that just adding a rule to pick up free text was conflicting
>>>>>> with matching everything else in the language so I added the following two
>>>>>> lines to the lexer :
>>>>>>
>>>>>> <text>.*\n               %{ this.popState(); return "TEXT"; %}
>>>>>> "__"                     %{ this.begin("text"); return "__"; %}
>>>>>>
>>>>>> I assume that __ now puts the lexer into a text mode, which then
>>>>>> allows the preceding rule to be applied.
>>>>>>
>>>>>> However jison blows up on this with the following errors :
>>>>>>
>>>>>> node.js:201
>>>>>>         throw e; // process.nextTick error, or 'error' event on first
>>>>>> tick
>>>>>>               ^
>>>>>> TypeError: Cannot read property 'rules' of undefined
>>>>>>     at prepareRules
>>>>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:42:47)
>>>>>>     at Object.buildActions
>>>>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:112:18)
>>>>>>     at Object.RegExpLexer
>>>>>> 
(/home/phil/local/node/lib/node_modules/jison/node_modules/jison-lex/regexp-lexer.js:131:39)
>>>>>>     at [object Object].Jison_Generator
>>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:108:22)
>>>>>>     at [object Object].<anonymous>
>>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:23:28)
>>>>>>     at new <anonymous>
>>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/util/typal.js:77:70)
>>>>>>     at new Jison_Generator
>>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/jison.js:1578:20)
>>>>>>     at processGrammar
>>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:87:21)
>>>>>>     at Object.main
>>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:61:56)
>>>>>>     at Object.<anonymous>
>>>>>> (/home/phil/local/node/lib/node_modules/jison/lib/cli.js:105:13)
>>>>>>
>>>>>> Anyone have an idea what I'm doing wrong?
>>>>>>
>>>>>> cheers
>>>>>>
>>>>>> Phil
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Robert Plummer
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Robert Plummer
>>>
>>>
>>>
>


-- 
Robert Plummer