How to describe "if" statement properly

58 views
Skip to first unread message

Alexander Kamyshnikov

unread,
Jun 25, 2017, 5:16:33 AM6/25/17
to PEG.js: Parser Generator for JavaScript
Hi, i'm working on Qt's qmake project file parser (open source project).
And i have a trouble with describing qmake's variant of conditional statement, called "scope" in documentation.

EBNF (simplified):

ScopeStatement -> Condition ScopeBody


Condition -> Identifier | TestFunctionCall | NotExpr | OrExpr | AndExpr
NotExpr -> "!" Condition
OrExpr   -> Condition "|" Condition
AndExpr -> Condition ":" Condition


ScopeBody -> COLON Statement | BR_OPEN Statement:*  BR_CLOSE


Statement -> AssignmentStatement
AssignmentStatement -> Identifier EQ String


// There are many others built-in boolean functions
TestFunctionCall -> ("defined" | ...)  ARG_LIST_OPEN (String COMMA:?):* ARG_LIST_CLOSE


Identifier -> Letter (Letter | Digit | UNDERSCP):+
String -> (Letter | Digit | UNDERSCP):+

EQ
-> "="
COLON
-> ":"
COMMA
-> ","
ARG_LIST_OPEN
-> "("
ARG_LIST_CLOSE
-> ")"
BLOCK_OPEN
-> "{"
BLOCK_CLOSE
-> "}"
UNDERSCP
-> "_"


First question: how to distinguish AND-operator colon from the condition terminal one? :(
Any examples of such kind of complex recursive grammar?

P.S. My grammar draft (without function call support) don't work even for simple case like
win32:xml: x = y

That's it code:

Start
 
= ScopeStatement


// ------------------------------------------------------------------------------
// qmake scope statement
ScopeStatement
 
= BooleanExpression ws* ((":" ws* SingleLineStatement) / ("{" ws* MultiLineStatement ))


SingleLineStatement
 
= Identifier ws* "=" ws* Identifier lb*


MultiLineStatement
 
= (SingleLineStatement lb*)+
 
// ------------------------------------------------------------------------------
// qmake condition statement
BooleanExpression
 
= BooleanOrExpression


BooleanOrExpression
 
= left:BooleanAndExpression ws* "|" ws* right:BooleanOrExpression !SingleLineStatement { return {type: "OR", left:left, right:right} }
 
/ BooleanAndExpression


BooleanAndExpression
 
= left:BooleanNotExpression ws* ":" ws* right:BooleanAndExpression !SingleLineStatement { return {type: "AND", left:left, right:right} }
 
/ BooleanNotExpression


BooleanNotExpression
 
= "!" ws* operand:BooleanNotExpression !SingleLineStatement { return {type: "NOT", operand: operand } }
 
/ BooleanComplexExpression


BooleanComplexExpression
 
= Identifier !SingleLineStatement
 
/ "(" logical_or:BooleanOrExpression ")" { return logical_or; }


// -------------------------------------------------------------------------------


Identifier
 
= token:[a-zA-Z0-9_]+ { return token.join(""); }


ws
 
= [ \t]


lb
 
= [\r\n]


Thanks

Guilherme Vieira

unread,
Jul 3, 2017, 12:52:55 PM7/3/17
to axil...@gmail.com, PEG.js: Parser Generator for JavaScript
Hi, Alexander!

I think the best way to get help around here is to provide your grammar, sample inputs, and expected vs. actual outputs.

If you could provide that, maybe I (or someone else) could help you.

Atenciosamente / Sincerely,
Guilherme Prá Vieira

--
You received this message because you are subscribed to the Google Groups "PEG.js: Parser Generator for JavaScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pegjs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Patterson

unread,
Jul 3, 2017, 1:18:42 PM7/3/17
to PEG.js: Parser Generator for JavaScript
The problem seems to be with the following rule in your test grammar

BooleanComplexExpression
  = Identifier !SingleLineStatement  
  / "(" logical_or:BooleanOrExpression ")" { return logical_or; }

this should be 
BooleanComplexExpression
  = !SingleLineStatement  Identifier 
  / "(" logical_or:BooleanOrExpression ")" { return logical_or; }

what you were saying with the original was 

find an Identifier not followed by SingleLineStatement

what you wanted to say was find an Identifier that is not part of a SingleLineStatement

Hope that helps you down the path.

Thanks,
Jason
Reply all
Reply to author
Forward
0 new messages