Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:04:39+00:00 2026-05-26T21:04:39+00:00

I am trying to define a simple functional language grammar, I am almost done

  • 0

I am trying to define a simple functional language grammar, I am almost done with my definitions, but I can’t get past the following ambiguities.

[14:43:53] warning(200): mygrammar.g:14:11: Decision can match input such as "ATOM" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input 

[14:43:53] warning(200): mygrammar.g:14:11: Decision can match input such as "ID" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input

here are what I think are the relevant rules the drawing is almost identical for ATOM and ID:

program : (statement'.')* ;

statement : assignment
          | expression;

assignment : func '->' statement ((','statement)=> ',' statement)*
           | ID '->' expression
           | ATOM '->' ( string | number ); 

func : (ID '(' args ')')=> ID '(' args ')'; 

term : func
     | '(' expression ')'  
     | number
     | string
     | ID
     | ATOM ;

ATOM : ('A'..'Z'|'_')+;

ID : ('a'..'z'|'_')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

here is the syntax tree from ANTLRWorks

id alternatives http://www.vertigrated.com/images/id_alternatives.png

Here is a rough straw man of what I am trying to support parsing.

hypotenuse(a,b) -> 
  sqr(x) -> x * x,
  sqr(sqr(a) + sqr(b)). 

print(hypotnenuse(2,3)).

so I need to be able to support nesting statements inside of functions

where -> is my assignment operator, this is a single assignment language

where . is my statement end marker

Is this even possible to parse with ANTLR3?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:04:40+00:00Added an answer on May 26, 2026 at 9:04 pm

    Some tips to make your grammar less ambiguous:

    1. remove the '.' as an end-of-statement char. Replace it with, say, a ';'. Both are just one character, and a ';' does not conflict with the '.' inside a float;
    2. create a lexer rule that matches a float;
    3. a function is followed by one or more statements, but has no obvious end (except for the “main” function which now ends with a '.'). You must either tell your parser where a function ends (not just the “main” function), or must introduce a rule that matches an “inner” function that does not have multiple trailing statements, but a single expression;
    4. do you really need a expression in your statement rule? Both can be a function (makein it, again, ambiguous).

    These are just a few of the problems with the way you’ve defined your language. If I were you, I wouldn’t continue this way: you might fix some things with predicates, but altering something on another place will open yet another can of worms. I advise you redesign your language drastically.

    If you don’t want to redesign it, simply remove all predicates from your grammar and put the following line in your options {...} section:

    backtrack=true;
    

    which will (seemingly) magically remove all warnings regarding ambiguous rules, but this is not a recommended fix. That way, ANTLR will choose parse trees for you when it encounters an ambiguity, (highly) possibly resulting in unexpected behavior, and for large input, it results in longer parse times. Again, this is not what I’d go for! See the Wiki for more info regarding global backtracking: http://www.antlr.org/wiki/display/ANTLR3/How+to+remove+global+backtracking+from+your+grammar

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to define a new shortcode in WordPress, and I get the following
I'm trying to get this simple windows dialog to spawn but as soon as
I am trying to define a basic function in python but I always get
I'm trying to define an action for a model Bar -- but I only
Having issues getting myself started on MVC3 Trying to define a simple route here
I'm trying to get a simple effect to display using WebGL. Naturally, this means
i'm trying to recreate the MVC model of their simple example but i'm having
The following code results in 'favlist is not defined'. Despite trying to define favlist
I'm trying to create a simple macro using varargs, but I'm getting va_start used
I'm trying to implement a simple stack. It works fine on MacOSX but crashes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.