Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated internal parser contains java syntax error #3296

Open
SimonCockx opened this issue Dec 16, 2024 · 4 comments
Open

Generated internal parser contains java syntax error #3296

SimonCockx opened this issue Dec 16, 2024 · 4 comments

Comments

@SimonCockx
Copy link

I'm seeing a syntax error in the generated InternalMyDslParser.java file:

        // InternalMyDsl.g:139:7: ( ( ( ',' ( ( ruleSwitchCaseOrDefault ) ) ) )=> ( ',' ( ( ruleSwitchCaseOrDefault ) ) ) )*
        loop:
        do {
            int alt=;  // SEE THIS LINE
            switch (alt) {
        	case 1 :
        	    ...
        	    break;
        	default :
        	    break loop;
            }
        } while (true);

Minimal repro grammar file:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Expression:
	expression=SwitchOperation;

SwitchOperation returns Expression:
	Integer
	(
		=>({SwitchOperation.argument=current} operator='switch' =>(cases+=SwitchCaseOrDefault =>(',' cases+=SwitchCaseOrDefault)*)?)
	)*
;

SwitchCaseOrDefault:
	'default' expression=Expression
 	| guard=Integer 'then' expression=Expression
;

Integer returns Expression:
	value=INT
;

Is there any workaround for this?

@szarnekow
Copy link
Contributor

Can you try to remove the nested predicates)

@szarnekow
Copy link
Contributor

Looks like they should be replaced by first-set predicates in all three cases. Use -> instead of the fat arrow

@SimonCockx
Copy link
Author

I'll try out your suggestion, thanks.

I wonder if this works for a more complex grammar as well though. I'll try it out on my actual grammar (see this line here: https://github.com/REGnosys/rune-dsl/blob/switch-fix/rosetta-lang/src/main/java/com/regnosys/rosetta/Rosetta.xtext#L676), and see if I need to "enhance" the example a bit to make more sense. I'll come back.

@SimonCockx
Copy link
Author

SimonCockx commented Dec 16, 2024

@szarnekow This did indeed fixes the issue. I did a review of my expression parser rules, and figured I could use a first token predicate -> most of the time, except in a few places. Thanks for that.

Should I close the issue, or keep it open, since the actual generation issue is not fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants