peg - Parsing constant and identifier -


i want match constant, uppercase string.

also, want match identifier, can contain mix of lowercase , uppercase letters.

start    = constant   / identifier  identifier   = h:[a-za-z_] t:[a-za-z0-9_]* { return { type: 'identifier', value: h + t.join('') } }  constant   = h:[a-z] t:[a-z_0-9]* { return { type: 'constant', value: h + t.join('') } } 

the problem is, when try match asd, says: line 1, column 2: expected [a-z_0-9] or end of input "s" found.

it seems matches constant rule doesn't swap identifier 1 when fails...

the problem seems constant valid identifier, can't figure out rules break ambiguity, think if constant match fails should try identifier rule...

the problem here happens because parsing expressions grammars not context free grammars. first match instead of backtracking. constant rule defined before identifier. asd matches start character constant rule, next char doesn't, therefore, throws error, because deterministic. hopefully, easy fix:

start    = constant   / identifier  identifier   = h:[a-za-z_] t:[a-za-z0-9_]* { return { type: 'identifier', value: h + t.join('') } }  constant   = h:[a-z] ![a-z] t:[a-z_0-9]* { return { type: 'constant', value: h + t.join('') } }   

outputs:

{    "type": "identifier",    "value": "asd" } 

pegs are, default, deterministic , avoid ambiguity, rule defines.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -