Convert a form of BNF grammar to g4 grammar
up vote
0
down vote
favorite
I'm trying to covert this BNF grammar for validating boolean expression which is currently being used in Swift. Now want to implement similar parser in Java. I came across Antlr4 lib and want to use it to generate parser for the same grammar. I'm not very familiar with antlr4. Can someone please provide me some guidance? This is what I have so far.
Expr ::= <ConcatenationExpr>;
NonInfixExpr ::= <BracketExpr>
| <Function>
| <Literal>
| <Accessor>;
BracketExpr ::= '(' <Expr> ')';
Accessor ::= ('$' <AccessorComponent> | <AccessorComponent>) ('.' <AccessorComponent> )*;
AccessorComponent ::= 'Identifier' ':' 'Identifier' | 'Identifier';
Function ::= 'Identifier' '(' ')' | ('Identifier' '(' <Expr> (',' <Expr>)* ')');
Literal ::= 'True'
| 'False'
| 'Null'
| 'Number'
| 'Text';
ConcatenationExpr ::= <AndExpr>
| <ConcatenationExpr> '&' <AndExpr>;
AndExpr ::= <OrExpr>
| <AndExpr> '&&' <OrExpr>;
OrExpr ::= <EqualityExpr>
| <OrExpr> '||' <EqualityExpr>;
EqualityExpr ::= <ComparisonExpr>
| <EqualityExpr> ('==' | '=' | '!=' | '<>') <ComparisonExpr>;
ComparisonExpr ::= <AddExpr>
| <AddExpr> ('<' | '<=' | '>' | '>=') <AddExpr>;
AddExpr ::= <ExponentialExpr>
| <AddExpr> ('+' | '-') <ExponentialExpr>;
ExponentialExpr ::= <MultExpr>
| <ExponentialExpr> '^' <MultExpr>;
MultExpr ::= <NonInfixExpr>
| <MultExpr> ('*' | '/') <NonInfixExpr>;
I tried to convert to g4 and this is what it looks like.
grammar VALIDATE;
Expr
: ConcatenationExpr ';'
;
NonInfixExpr
: BracketExpr
| Function
| Literal
| Accessor
;
BracketExpr
: '(' Expr ')'
;
Accessor
: ('$' AccessorComponent | AccessorComponent ) ('.' AccessorComponent )*
;
AccessorComponent
: 'Identifier' ':' 'Identifier' | 'Identifier'
;
Function
: 'Identifier' '(' ')' | ('Identifier' '(' Expr (',' Expr)* ')')
;
Literal
: 'True' | 'False' | 'Null' | 'Number' | 'Text'
;
ConcatenationExpr
: AndExpr
| ConcatenationExpr '&&' AndExpr
;
AndExpr
: OrExpr | AndExpr '&&' OrExpr
;
OrExpr
: EqualityExpr | OrExpr '||' EqualityExpr
;
EqualityExpr
: ComparisonExpr | EqualityExpr ('==' | '=' | '!=' | '<>') ComparisonExpr
;
ComparisonExpr
: AddExpr | AddExpr ('<' | '<=' | '>' | '>=') AddExpr
;
AddExpr
: ExponentialExpr | AddExpr ('+' | '-') ExponentialExpr
;
ExponentialExpr
: MultExpr | ExponentialExpr '^' MultExpr
;
MultExpr
: NonInfixExpr | MultExpr ('*' | '/') NonInfixExpr
;
I'm stuck at antlr4 VALIDATE.g4 step. I'm not sure I did the conversion right.
error(119): VALIDATE.g4::: The following sets of rules are mutually left-recursive [MultExpr] and [ExponentialExpr] and [AddExpr] and [EqualityExpr] and [OrExpr] and [AndExpr] and [ConcatenationExpr]
error(99): VALIDATE.g4::: grammar VALIDATE has no rules
java parsing antlr4 parser-generator
add a comment |
up vote
0
down vote
favorite
I'm trying to covert this BNF grammar for validating boolean expression which is currently being used in Swift. Now want to implement similar parser in Java. I came across Antlr4 lib and want to use it to generate parser for the same grammar. I'm not very familiar with antlr4. Can someone please provide me some guidance? This is what I have so far.
Expr ::= <ConcatenationExpr>;
NonInfixExpr ::= <BracketExpr>
| <Function>
| <Literal>
| <Accessor>;
BracketExpr ::= '(' <Expr> ')';
Accessor ::= ('$' <AccessorComponent> | <AccessorComponent>) ('.' <AccessorComponent> )*;
AccessorComponent ::= 'Identifier' ':' 'Identifier' | 'Identifier';
Function ::= 'Identifier' '(' ')' | ('Identifier' '(' <Expr> (',' <Expr>)* ')');
Literal ::= 'True'
| 'False'
| 'Null'
| 'Number'
| 'Text';
ConcatenationExpr ::= <AndExpr>
| <ConcatenationExpr> '&' <AndExpr>;
AndExpr ::= <OrExpr>
| <AndExpr> '&&' <OrExpr>;
OrExpr ::= <EqualityExpr>
| <OrExpr> '||' <EqualityExpr>;
EqualityExpr ::= <ComparisonExpr>
| <EqualityExpr> ('==' | '=' | '!=' | '<>') <ComparisonExpr>;
ComparisonExpr ::= <AddExpr>
| <AddExpr> ('<' | '<=' | '>' | '>=') <AddExpr>;
AddExpr ::= <ExponentialExpr>
| <AddExpr> ('+' | '-') <ExponentialExpr>;
ExponentialExpr ::= <MultExpr>
| <ExponentialExpr> '^' <MultExpr>;
MultExpr ::= <NonInfixExpr>
| <MultExpr> ('*' | '/') <NonInfixExpr>;
I tried to convert to g4 and this is what it looks like.
grammar VALIDATE;
Expr
: ConcatenationExpr ';'
;
NonInfixExpr
: BracketExpr
| Function
| Literal
| Accessor
;
BracketExpr
: '(' Expr ')'
;
Accessor
: ('$' AccessorComponent | AccessorComponent ) ('.' AccessorComponent )*
;
AccessorComponent
: 'Identifier' ':' 'Identifier' | 'Identifier'
;
Function
: 'Identifier' '(' ')' | ('Identifier' '(' Expr (',' Expr)* ')')
;
Literal
: 'True' | 'False' | 'Null' | 'Number' | 'Text'
;
ConcatenationExpr
: AndExpr
| ConcatenationExpr '&&' AndExpr
;
AndExpr
: OrExpr | AndExpr '&&' OrExpr
;
OrExpr
: EqualityExpr | OrExpr '||' EqualityExpr
;
EqualityExpr
: ComparisonExpr | EqualityExpr ('==' | '=' | '!=' | '<>') ComparisonExpr
;
ComparisonExpr
: AddExpr | AddExpr ('<' | '<=' | '>' | '>=') AddExpr
;
AddExpr
: ExponentialExpr | AddExpr ('+' | '-') ExponentialExpr
;
ExponentialExpr
: MultExpr | ExponentialExpr '^' MultExpr
;
MultExpr
: NonInfixExpr | MultExpr ('*' | '/') NonInfixExpr
;
I'm stuck at antlr4 VALIDATE.g4 step. I'm not sure I did the conversion right.
error(119): VALIDATE.g4::: The following sets of rules are mutually left-recursive [MultExpr] and [ExponentialExpr] and [AddExpr] and [EqualityExpr] and [OrExpr] and [AndExpr] and [ConcatenationExpr]
error(99): VALIDATE.g4::: grammar VALIDATE has no rules
java parsing antlr4 parser-generator
ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single ruleExpr
with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.
– sepp2k
Nov 11 at 4:11
1
"Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
– Stephen C
Nov 11 at 6:22
@StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
– sepp2k
Nov 11 at 14:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to covert this BNF grammar for validating boolean expression which is currently being used in Swift. Now want to implement similar parser in Java. I came across Antlr4 lib and want to use it to generate parser for the same grammar. I'm not very familiar with antlr4. Can someone please provide me some guidance? This is what I have so far.
Expr ::= <ConcatenationExpr>;
NonInfixExpr ::= <BracketExpr>
| <Function>
| <Literal>
| <Accessor>;
BracketExpr ::= '(' <Expr> ')';
Accessor ::= ('$' <AccessorComponent> | <AccessorComponent>) ('.' <AccessorComponent> )*;
AccessorComponent ::= 'Identifier' ':' 'Identifier' | 'Identifier';
Function ::= 'Identifier' '(' ')' | ('Identifier' '(' <Expr> (',' <Expr>)* ')');
Literal ::= 'True'
| 'False'
| 'Null'
| 'Number'
| 'Text';
ConcatenationExpr ::= <AndExpr>
| <ConcatenationExpr> '&' <AndExpr>;
AndExpr ::= <OrExpr>
| <AndExpr> '&&' <OrExpr>;
OrExpr ::= <EqualityExpr>
| <OrExpr> '||' <EqualityExpr>;
EqualityExpr ::= <ComparisonExpr>
| <EqualityExpr> ('==' | '=' | '!=' | '<>') <ComparisonExpr>;
ComparisonExpr ::= <AddExpr>
| <AddExpr> ('<' | '<=' | '>' | '>=') <AddExpr>;
AddExpr ::= <ExponentialExpr>
| <AddExpr> ('+' | '-') <ExponentialExpr>;
ExponentialExpr ::= <MultExpr>
| <ExponentialExpr> '^' <MultExpr>;
MultExpr ::= <NonInfixExpr>
| <MultExpr> ('*' | '/') <NonInfixExpr>;
I tried to convert to g4 and this is what it looks like.
grammar VALIDATE;
Expr
: ConcatenationExpr ';'
;
NonInfixExpr
: BracketExpr
| Function
| Literal
| Accessor
;
BracketExpr
: '(' Expr ')'
;
Accessor
: ('$' AccessorComponent | AccessorComponent ) ('.' AccessorComponent )*
;
AccessorComponent
: 'Identifier' ':' 'Identifier' | 'Identifier'
;
Function
: 'Identifier' '(' ')' | ('Identifier' '(' Expr (',' Expr)* ')')
;
Literal
: 'True' | 'False' | 'Null' | 'Number' | 'Text'
;
ConcatenationExpr
: AndExpr
| ConcatenationExpr '&&' AndExpr
;
AndExpr
: OrExpr | AndExpr '&&' OrExpr
;
OrExpr
: EqualityExpr | OrExpr '||' EqualityExpr
;
EqualityExpr
: ComparisonExpr | EqualityExpr ('==' | '=' | '!=' | '<>') ComparisonExpr
;
ComparisonExpr
: AddExpr | AddExpr ('<' | '<=' | '>' | '>=') AddExpr
;
AddExpr
: ExponentialExpr | AddExpr ('+' | '-') ExponentialExpr
;
ExponentialExpr
: MultExpr | ExponentialExpr '^' MultExpr
;
MultExpr
: NonInfixExpr | MultExpr ('*' | '/') NonInfixExpr
;
I'm stuck at antlr4 VALIDATE.g4 step. I'm not sure I did the conversion right.
error(119): VALIDATE.g4::: The following sets of rules are mutually left-recursive [MultExpr] and [ExponentialExpr] and [AddExpr] and [EqualityExpr] and [OrExpr] and [AndExpr] and [ConcatenationExpr]
error(99): VALIDATE.g4::: grammar VALIDATE has no rules
java parsing antlr4 parser-generator
I'm trying to covert this BNF grammar for validating boolean expression which is currently being used in Swift. Now want to implement similar parser in Java. I came across Antlr4 lib and want to use it to generate parser for the same grammar. I'm not very familiar with antlr4. Can someone please provide me some guidance? This is what I have so far.
Expr ::= <ConcatenationExpr>;
NonInfixExpr ::= <BracketExpr>
| <Function>
| <Literal>
| <Accessor>;
BracketExpr ::= '(' <Expr> ')';
Accessor ::= ('$' <AccessorComponent> | <AccessorComponent>) ('.' <AccessorComponent> )*;
AccessorComponent ::= 'Identifier' ':' 'Identifier' | 'Identifier';
Function ::= 'Identifier' '(' ')' | ('Identifier' '(' <Expr> (',' <Expr>)* ')');
Literal ::= 'True'
| 'False'
| 'Null'
| 'Number'
| 'Text';
ConcatenationExpr ::= <AndExpr>
| <ConcatenationExpr> '&' <AndExpr>;
AndExpr ::= <OrExpr>
| <AndExpr> '&&' <OrExpr>;
OrExpr ::= <EqualityExpr>
| <OrExpr> '||' <EqualityExpr>;
EqualityExpr ::= <ComparisonExpr>
| <EqualityExpr> ('==' | '=' | '!=' | '<>') <ComparisonExpr>;
ComparisonExpr ::= <AddExpr>
| <AddExpr> ('<' | '<=' | '>' | '>=') <AddExpr>;
AddExpr ::= <ExponentialExpr>
| <AddExpr> ('+' | '-') <ExponentialExpr>;
ExponentialExpr ::= <MultExpr>
| <ExponentialExpr> '^' <MultExpr>;
MultExpr ::= <NonInfixExpr>
| <MultExpr> ('*' | '/') <NonInfixExpr>;
I tried to convert to g4 and this is what it looks like.
grammar VALIDATE;
Expr
: ConcatenationExpr ';'
;
NonInfixExpr
: BracketExpr
| Function
| Literal
| Accessor
;
BracketExpr
: '(' Expr ')'
;
Accessor
: ('$' AccessorComponent | AccessorComponent ) ('.' AccessorComponent )*
;
AccessorComponent
: 'Identifier' ':' 'Identifier' | 'Identifier'
;
Function
: 'Identifier' '(' ')' | ('Identifier' '(' Expr (',' Expr)* ')')
;
Literal
: 'True' | 'False' | 'Null' | 'Number' | 'Text'
;
ConcatenationExpr
: AndExpr
| ConcatenationExpr '&&' AndExpr
;
AndExpr
: OrExpr | AndExpr '&&' OrExpr
;
OrExpr
: EqualityExpr | OrExpr '||' EqualityExpr
;
EqualityExpr
: ComparisonExpr | EqualityExpr ('==' | '=' | '!=' | '<>') ComparisonExpr
;
ComparisonExpr
: AddExpr | AddExpr ('<' | '<=' | '>' | '>=') AddExpr
;
AddExpr
: ExponentialExpr | AddExpr ('+' | '-') ExponentialExpr
;
ExponentialExpr
: MultExpr | ExponentialExpr '^' MultExpr
;
MultExpr
: NonInfixExpr | MultExpr ('*' | '/') NonInfixExpr
;
I'm stuck at antlr4 VALIDATE.g4 step. I'm not sure I did the conversion right.
error(119): VALIDATE.g4::: The following sets of rules are mutually left-recursive [MultExpr] and [ExponentialExpr] and [AddExpr] and [EqualityExpr] and [OrExpr] and [AndExpr] and [ConcatenationExpr]
error(99): VALIDATE.g4::: grammar VALIDATE has no rules
java parsing antlr4 parser-generator
java parsing antlr4 parser-generator
asked Nov 11 at 4:06
Abhijit Parate
1
1
ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single ruleExpr
with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.
– sepp2k
Nov 11 at 4:11
1
"Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
– Stephen C
Nov 11 at 6:22
@StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
– sepp2k
Nov 11 at 14:37
add a comment |
ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single ruleExpr
with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.
– sepp2k
Nov 11 at 4:11
1
"Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
– Stephen C
Nov 11 at 6:22
@StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
– sepp2k
Nov 11 at 14:37
ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single rule
Expr
with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.– sepp2k
Nov 11 at 4:11
ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single rule
Expr
with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.– sepp2k
Nov 11 at 4:11
1
1
"Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
– Stephen C
Nov 11 at 6:22
"Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
– Stephen C
Nov 11 at 6:22
@StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
– sepp2k
Nov 11 at 14:37
@StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
– sepp2k
Nov 11 at 14:37
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245751%2fconvert-a-form-of-bnf-grammar-to-g4-grammar%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single rule
Expr
with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.– sepp2k
Nov 11 at 4:11
1
"Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
– Stephen C
Nov 11 at 6:22
@StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
– sepp2k
Nov 11 at 14:37