Quantcast

Expression parser idea

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Expression parser idea

MichaelGolovanov
Hi, guys

I am not scala and parboiled expert, but try to use parboiled for math expressions parser and have idea for discussion.

Most of programming langs operate with two operand expressions like primitive-whitespace-operator-whitespace-primitive. Examples: 12 + 7, 122 < 200.
Most of this need operation priority and parens: (10 + 5)*2

The idea is to unify parboiled parser for this. Parser`s descendants need to declare only primitive and operations list (operators with high priority should be first in list).

Example:
class MathParser extends ExpressionParser {
  def Primitive = rule { oneOrMore("0" - "9") }
  def Operations = List("*/", "+-")
}

ExpressionParser is unified parser, that do all work to support whitespaces and operation priority with parens. Source code is uploaded with this message ExpressionParser.scala

Is this right way or i am mistaken?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Expression parser idea

mathias
Administrator
Thanks, Michael, for this contribution.

I'm generally hesitant to include specializations of the kind you proposed directly into parboiled.
parboiled is a general parsing framework and allows you to implement _any_ kind of grammar.

Which kind of grammars would be able to directly use your ExpressionParser?
I think that in most applications people will actually want to create some kind of AST for their grammar, which would be difficult to integrate into your specialization.
Also the rules that you included into your ExpressionParser are not really _that_ complex and would be rather easy to implement with my own parser, so the value-add from such a specialization would be somewhat limited, no?

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 02.01.2012, at 18:16, MichaelGolovanov [via parboiled users] wrote:

> Hi, guys
>
> I am not scala and parboiled expert, but try to use parboiled for math expressions parser and have idea for discussion.
>
> Most of programming langs operate with two operand expressions like primitive-whitespace-operator-whitespace-primitive. Examples: 12 + 7, 122 < 200.
> Most of this need operation priority and parens: (10 + 5)*2
>
> The idea is to unify parboiled parser for this. Parser`s descendants need to declare only primitive and operations list (operators with high priority should be first in list).
>
> Example:
> class MathParser extends ExpressionParser {
>   def Primitive = rule { oneOrMore("0" - "9") }
>   def Operations = List("*/", "+-")
> }
>
> ExpressionParser is unified parser, that do all work to support whitespaces and operation priority with parens. Source code is uploaded with this message ExpressionParser.scala
>
> Is this right way or i am mistaken?
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Expression-parser-idea-tp3627074p3627074.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

fge
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Expression parser idea

fge
On Tue, Jan 24, 2012 at 11:14, mathias [via parboiled users]
<[hidden email]> wrote:
> Thanks, Michael, for this contribution.
>
> I'm generally hesitant to include specializations of the kind you proposed
> directly into parboiled.
> parboiled is a general parsing framework and allows you to implement _any_
> kind of grammar.
>

Why not a "parboiled-sugar" package then?

--
Francis Galiegue, [hidden email]
"It seems obvious [...] that at least some 'business intelligence'
tools invest so much intelligence on the business side that they have
nothing left for generating SQL queries" (St├ęphane Faroult, in "The
Art of SQL", ISBN 0-596-00894-5)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Expression parser idea

MichaelGolovanov
In reply to this post by mathias
Hi, Mathias

mathias wrote
I'm generally hesitant to include specializations of the kind you proposed directly into parboiled.
parboiled is a general parsing framework and allows you to implement _any_ kind of grammar.
No problems, its a reasonable. And thanx to fge for idea with parboiled-sugar.


mathias wrote
Which kind of grammars would be able to directly use your ExpressionParser?
I think that in most applications people will actually want to create some kind of AST for their grammar, which would be difficult to integrate into your specialization.
Thank you, i should be thinking about AST. Good point.


mathias wrote
Also the rules that you included into your ExpressionParser are not really _that_ complex and would be rather easy to implement with my own parser, so the value-add from such a specialization would be somewhat limited, no?
Really its a very limited specialization. But i spend some hours to developing simple math parser with whitespace support and want to generalize and share small piece of my job. Most of time i carefully read parboiled wiki (handling whitespaces page) and samples source code about whitespaces support.

Should be nice to include more info and scala samples to demonstrate how to handle whitespaces (because its a very common task). How can i help with this?

With best regards, Michael
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Expression parser idea

mathias
Administrator
Michael,

> Should be nice to include more info and scala samples to demonstrate how to handle whitespaces (because its a very common task). How can i help with this?

one good way would be to put an example up on github that can then be linked to from the parboiled documentation.
I think non-trivial real-world examples can be of great value to quite a large number of users...

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 25.01.2012, at 15:58, MichaelGolovanov [via parboiled users] wrote:

> Hi, Mathias
>
> mathias wrote
> I'm generally hesitant to include specializations of the kind you proposed directly into parboiled.
> parboiled is a general parsing framework and allows you to implement _any_ kind of grammar.
> No problems, its a reasonable. And thanx to fge for idea with parboiled-sugar.
>
>
> mathias wrote
> Which kind of grammars would be able to directly use your ExpressionParser?
> I think that in most applications people will actually want to create some kind of AST for their grammar, which would be difficult to integrate into your specialization.
> Thank you, i should be thinking about AST. Good point.
>
>
> mathias wrote
> Also the rules that you included into your ExpressionParser are not really _that_ complex and would be rather easy to implement with my own parser, so the value-add from such a specialization would be somewhat limited, no?
> Really its a very limited specialization. But i spend some hours to developing simple math parser with whitespace support and want to generalize and share small piece of my job. Most of time i carefully read parboiled wiki (handling whitespaces page) and samples source code about whitespaces support.
>
> Should be nice to include more info and scala samples to demonstrate how to handle whitespaces (because its a very common task). How can i help with this?
>
> With best regards, Michael
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Expression-parser-idea-tp3627074p3687903.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Loading...