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 133173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:23:01+00:00 2026-05-11T06:23:01+00:00

I have the following EBNF that I want to parse: PostfixExp -> PrimaryExp (

  • 0

I have the following EBNF that I want to parse:

PostfixExp      -> PrimaryExp ( '[' Exp ']'                                  | . id '(' ExpList ')'                                  | . length )* 

And this is what I got:

def postfixExp: Parser[Expression] = (     primaryExp ~ rep(         '[' ~ expression ~ ']'         | '.' ~ ident ~'(' ~ repsep(expression, ',' ) ~ ')'          | '.' ~ 'length') ^^ {         case primary ~ list =>  list.foldLeft(primary)((prim,post) =>                 post match {                     case '[' ~ length ~ ']' => ElementExpression(prim, length.asInstanceOf[Expression])                     case '.' ~ function ~'(' ~ arguments ~ ')' =>  CallMethodExpression(prim, function.asInstanceOf[String], arguments.asInstanceOf[List[Expression]])                     case _ => LengthExpression(prim)                 }             )     }) 

But I would like to know if there is a better way, preferably without having to resort to casting (asInstanceOf).

  • 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. 2026-05-11T06:23:01+00:00Added an answer on May 11, 2026 at 6:23 am

    I would do it like this:

    type E = Expression  def postfixExp = primaryExp ~ rep(     '[' ~> expr <~ ']' ^^ { e => ElementExpression(_:E, e) }   | '.' ~ 'length' ^^^ LengthExpression   | '.' ~> ident ~ ('(' ~> repsep(expr, ',') <~ ')') ^^ flatten2 { (f, args) =>       CallMethodExpression(_:E, f, args)     } ) ^^ flatten2 { (e, ls) => collapse(ls)(e) }  def expr: Parser[E] = ...  def collapse(ls: List[E=>E])(e: E) = {   ls.foldLeft(e) { (e, f) => f(e) } } 

    Shortened expressions to expr for brevity as well as added the type alias E for the same reason.

    The trick that I’m using here to avoid the ugly case analysis is to return a function value from within the inner production. This function takes an Expression (which will be the primary) and then returns a new Expression based on the first. This unifies the two cases of dot-dispatch and bracketed expressions. Finally, the collapse method is used to merge the linear List of function values into a proper AST, starting with the specified primary expression.

    Note that LengthExpression is just returned as a value (using ^^^) from its respective production. This works because the companion objects for case classes (assuming that LengthExpression is indeed a case class) extend the corresponding function value delegating to their constructor. Thus, the function represented by LengthExpression takes a single Expression and returns a new instance of LengthExpression, precisely satisfying our needs for the higher-order tree construction.

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

Sidebar

Related Questions

I have an EBNF grammar that has a few rules with this pattern: sequence
I have following type of data and I want to produce bar graph. Mark
I have following asp hyperlink: <asp:HyperLink ID=a runat=server Text=return NavigateUrl=https://google.com/></asp:HyperLink > What i want
I have following code that creates Linq query. I've never used Linq until today
I have following table it shows like this: ABC A B C 123 Hello
I have following json data which I want to pass it to server using
I have following link in a.aspx file: <a href=a.htm target=iframe>A</a> I want to set
I have following example string that needs to be filtered 0173556677 (Alice), 017545454 (Bob)
I have following url: http://localhost.com/phpdemo/bid/tf/red ? This url redirects through This [ $route['tf/red?'] =
I have following data: Dictionary<string,string> dctParameters = new Dictionary(){ {a,var1},{b,var2},{c,var3},.... } I want to

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.