Eliminate left recursion in XText - recursion

This is part of the grammar of the NuSMV language:
BasicExpression:
Constant | '(' BasicExpression ')' | '!' BasicExpression | BasicExpression '&' BasicExpression;
Constant:
BooleanConstant
BooleanConstant:
'TRUE' | 'FALSE';
Unfortunately XText is throwing an exception that states there is left recursion in this grammar.
How can I fix it?
Thanks.

You could simply introduce a new rule (a new tier) like that:
BasicExpression:
firstContent=ExpressionContent ("&" secondContent=ExpressionContent)?
;
ExpressionContent:
Constant
| '(' BasicExpression ')'
| '!' BasicExpression
;
That way the rule is not left recursive anymore.
Greeting Krzmbrzl

Have a look at this article or the official documentation, it will explain in detail how to handle recursion in language, and taking into account operator precedence.

Related

Prolog: uncaught exception: error(syntax_error('user_input:4 (char:7)

I get the following error message when I try to run my Prolog code. I am very new to this language and am having a lot of trouble finding my error. I am using the GNU Prolog compiler.(Version 1.4.4 by Daniel Diaz) I believe the error has something to do with the list or the member predicate. Can you please help me?
uncaught exception: error(syntax_error('user_input:4 (char:7) expression expected'),read_term/3)
Here is my prolog code.
alien(flubsub).
alien(gribbet).
alien(jarix).
alien(wattin).
child(andrew).
child(dudley).
child(georgina).
child(karen).
features(bubbles).
features(colors).
features(eyes).
features(fins).
solve :-
alien(andrewAlien), alien(dudleyAlien), alien(georginaAlien),alien(karenAlien),
all_different([andrewAlien, dudleyAlien, georginaAlien, karenAlien]),
feature(andrewFeature), feature(dudleyFeature), feature(georginaFeature), feature(karenFeature),
all_different([andrewFeature, dudleyFeature,georginaFeature,karenFeature]),
Owners = [ [andrew,andrewAlien,andrewFeature],
[dudley,dudleyAlien,dudleyFeature],
[georgina,georginaAlien,georginaFeature],
[karen,karenAlien,karenFeature]
],
% 1. Dudley didn't walk out of the store with either Flubsub or Jarix, and his alien doesn't develop fins when placed in water.
\+ member([dudley,flubsub,_],Owners),
\+ member([dudley,jarix,_],Owners),
\+ member([dudley,_,fins],Owners),
%2. Jarix (which isn't the name of the alien Andrew picked) has eyes that glow in the dark.
\+ member([andrew,jarix,eyes],Owners),
member([_,jarix,eyes],Owners),
%3 Karen left the toy store with the alien Wattin
member([karen,wattin,_],Owners),
%4 Andrew doesn't own the alien that develops fins and Dudley doesn't own the alien that blows bubbles
\+ member([andrew,_,fins],Owners),
\+ member([dudley,_,bubbles],Owners),
tell(andrew,andrewAlien,andrewFeature),
tell(dudley,dudleyAlien,dudleyFeature),
tell(georgina,georginaAlien,georginaFeature),
tell(karen,karenAlien,karenFeature).
%Succeeds if all elements of the argument list are bound and different
%Fails if any elements are unbound or equal to some other element
all_different([H | T]) :- member(H, T), !, fail.
all_different([_ | T]) :- all_different(T).
all_different([_]).
tell(x, y, z) :-
write(' '), write(x), write( ' got the '), write(y),
write(' with feature '), write(z), write('.'),nl.

Can the F# compiler optimize these mutually recursive functions?

I wrote the following function that checks the validity of bracketed expressions:
let matched str =
let rec matched' stack = function
| "" -> isEmpty stack
| str ->
match first str with
| '(' | '[' | '{' as p -> matched' (push p stack) (rest str)
| ')' -> matchClosing '(' stack str
| ']' -> matchClosing '[' stack str
| '}' -> matchClosing '{' stack str
| _ -> matched' stack (rest str)
and matchClosing expected stack s =
match peek stack with
| Some c when c = expected -> matched' (pop stack) (rest s)
| _ -> false
matched' [] str
If we substitute the implementation of matchClosing into matched', we get a tail recursive function. Can the F# compiler recognize this and optimize away the recursive calls?
AFAICT your example isn't complete which makes it harder to check. I complemented it somewhat and was able to compile it.
Using ILSpy one sees that the mutual recursion is still in place:
// F#: | ')' -> matchClosing '(' stack str
case ')':
return Program.matchClosing#39('(', stack, str);
// F#: | matched' t (tail s)
return Program.matched'#28(t, s.Tail);
So while it should be technically possible to unpack two mutually tail recursive function into a loop it's not done.
When checking the IL code we see that the the calls are tagged with .tail
// F#: | matchClosing '(' stack str
IL_0083: tail. // Here
IL_0085: call bool Program::matchClosing#39(char, class [FSharp.Core]Microsoft.FSharp.Collections.FSharpList`1<char>, valuetype Program/SubString)
// F#: | matched' t (tail s)
IL_002a: tail. // Here
IL_002c: call bool Program::'matched\'#28'(class [FSharp.Core]Microsoft.FSharp.Collections.FSharpList`1<char>, valuetype Program/SubString)
The .NET Jitter is in release mode kind enough to consider .tail flag
// As you can see when debugging the code in WinDbg
02410bdf e8fbd3176b call clr!JIT_TailCall (6d58dfdf)
We also see when we debug in WinDbg that the stack don't grow. Unfortunately when looking at clr!JIT_TailCall it does a fair amount of work meaning while it doesn't consume stack it consumes clock cycles instead like noted here: How to eliminate time spent in JIT_TailCall for functions that are genuinely non-recursive
However in Debug mode (and at least older versions of Mono) .tail flag is ignored
// As you can see when debugging the code in WinDbg (this is a normal call)
02f619c1 e8c2f4ffff call 02f60e88
We also see when we debug in WinDbg that the stack grow.
So the answer to your question should be:
No, the F# compiler isn't able to transform the mutually tail recursive calls into a loop.
However, the F# compiler tags the calls with a .tail attribute
The Release mode JIT:er kindly considers the .tail attributes and generates tail calls that don't grow the stack (but are ineffecient)
In Debug mode (and possibly mono) .tail attributes are ignored and no tail calls are generated by the JIT:er and the stack will grow.

How to write the syntax grammar of this diagram

I am trying to write the syntax grammar of the below diagram but I didn't succeed.
I tried
Choice ::= letter ( ( 'digit' | '_' ) letter )
but it is totally wrong.
How can I write the Syntax of this?
The right syntax in EBNF like notation would be:
<name> = <letter> (<letter> | <digit> | <underscore>)*

How to solve this grammar recursion?

I found this grammar for a calculator:
<Expression> ::= <ExpressionGroup> | <BinaryExpression> | <UnaryExpression> | <LiteralExpression>
<ExpressionGroup> ::= '(' <Expression> ')'
<BinaryExpression> ::= <Expression> <BinaryOperator> <Expression>
<UnaryExpression> ::= <UnaryOperator> <Expression>
<LiteralExpression> ::= <RealLiteral> | <IntegerLiteral>
<BinaryOperator> ::= '+' | '-' | '/' | '*'
<UnaryOperator> ::= '+' | '-'
<RealLiteral> ::= <IntegerLiteral> '.' | <IntegerLiteral> '.' <IntegerLiteral>
<IntegerLiteral> ::= <Digit> <IntegerLiteral> | <Digit>
<Digit> ::= '0' | '1' |'2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
Source: here
It looks great. So I wrote the lexer and started the parser. Now there is an infinite recursion that I can't solve between Expression and BinaryExpression.
My code for expression:
boolean isExpression() {
if (isExpressionGroup() || isBinaryExpression() || isUnaryExpression() || isLiteralExpression()) {
println("Expression!");
return true;
}
println("Not expression.");
return false;
}
And for binary expression:
boolean isBinaryExpression() {
if (isExpression()) {
peek(1);
if (currentLex.token == Token.BINARY_OPERATOR) {
peek(2);
if (isExpression()) {
peek(3);
println("Binary expression!");
return true;
} else peek(0);
} else peek(0);
} else peek(0);
return false;
}
So peek(int) is just a function for looking forward without consuming any lexemes. So my problem: My input is '2*3' . isExpression() gets called. isExpressionGroup() fails, because there is no '('. Then the isBinaryExpression() gets called, which calls isExpression(). isExpressionGroup() fails again, and isBinaryExpression() gets called again. And so on, until a stack overflow.
I know, there is ANTLR and JavaCC (and other tools), but I would like to do it without them.
Could anyone give a hand?
Dealing with left recursion in a hand-crafted top-descent parser is not easy. Parser generators that solve the problem have years of work in them. There are theoretical reasons for that.
The best solution if you don't want to use a tool is to eliminate the left recursion. The problem if you do it "by the book" is that you'll get an ugly grammar and an ugly parser that will be difficult to use.
But there's another solution. You can add enough rules to represent the precedence hierarchy of the operators, which is something you'd have to do anyway, unless you want to risk a a+b*c be parsed as (a+b)*c.
There are plenty of examples of non left-recursive grammars for expressions on the Web, and here in SO in particular. I suggest you take one of them, and start from there.

Antlr and left-recursive rules

I am trying to write a grammar using ANTLR, but I can't understand how antlr works with recursive choices.
I read lots of articles and forums, but can't solve my problem...
Here is a small part of my grammar:
grammar MyGrammar;
ComponentRef :
IDENT ('[' Expression (',' Expression)* ']')?
;
Expression:
ComponentRef ('(' FunctionArguments ')')?
;
FunctionArguments:
Expression (',' Expression)*
;
IDENT: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
I still don't understand why it doesn't work... there is no ambiguity! Isn't it?
Here is some examples of code my grammar should work with :
a
a[b,c]
a[b[c], d]
func(a)
func(a,b,c)
func[a](b,c)
func(a[b], c[d])
func[a](b[c])
Thank you by advance!
First, be sure to understand lexer and parser rules. Also read the ANTLR Mega Tutorial.
The code only uses lexer rules, which won't work. Although even lexer rules can be recursive (in ANTLR grammars), they are best avoided. Rather, most rules should be parser rules:
componentRef :
IDENT ('[' expression (',' expression)* ']')?
;
expression:
componentRef ('(' functionArguments ')')?
;
functionArguments:
expression (',' expression)*
;
IDENT: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
The grammar above won't recognize the input you've posted, but there are no error anymore. A grammar that recognizes the input you've posted, could look like this (untested!) grammar:
parse
: expr* EOF
;
expr
: IDENT (index | call)*
;
index
: '[' expr_list ']'
;
call
: '(' expr_list ')'
;
expr_list
: expr (',' expr)*
;
IDENT
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
;
SPACE
: (' ' | '\t' | '\r' | '\n')+ {skip();}
;
I'm assuming your capitol Expressions is an error. You probably meant to type lowercase.
How can you say there's no ambiguity? expressions call functionArguments, functionArguments calls expressions. -1

Resources