MGrammar for parsing IF statement - oslo

I am building up a MGrammar spec to parse some pseudo code looking for particular bits of information. I have most of the spec working except for 1 cruical element.
The pseudo code supports an if-then-else syntax and I have been unable to find a satisfactory way of parsing it. The exact construct is...
IF expression operator expression THEN
Statement1
Statement2
Statementn
ELSEIF expression operator expression THEN
Statement1
Statement2
Statementn
ELSE
Statement1
Statement2
Statementn
ENDIF
...Where the Else and Elseif are optional.
What I have so far is:
`syntax Statement = r:ReturnClause => r |
i:IfClause => i |
ei:ElseifClause => ei |
e:ElseClause => e |
end:EndClause => end |
v:Expression => v ;
syntax IfClause = If name:Identifier operator:Operator Then statement:Statement => If[name, operator, Then[statement]];
syntax ElseifClause = Elseif name:Identifier operator:Operator Then statement:Statement => ElseIf[name, operator, Then[statement]];
syntax ElseClause = Else statement:Statement => Else[statement];
syntax EndClause = Endif; `
However, the Statement after the 'Then' and 'Else' is not greedy enough and only captures the first statement in the parse tree.
Has anyone tried to implement the parsing of an If statement using MGrammar or have any suggestions??

You can find a (almost) complete C# 4.0 grammar in the archetype project on codeplex. Dan Vanderboom wrote it in preparation of his new language code named "Archetype".
Maybe that helps: http://archetype.codeplex.com/

Related

What is the exact difference between the expression and the statement in programming [duplicate]

In Python, what is the difference between expressions and statements?
Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators, the function call operator () the subscription operator [] and similar, and can be reduced to some kind of "value", which can be any Python object. Examples:
3 + 5
map(lambda x: x*x, range(10))
[a.x for a in some_iterable]
yield 7
Statements (see 1, 2), on the other hand, are everything that can make up a line (or several lines) of Python code. Note that expressions are statements as well. Examples:
# all the above expressions
print 42
if x: do_y()
return
a = 7
Expression -- from the New Oxford American Dictionary:
expression: Mathematics a collection
of symbols that jointly express a
quantity : the expression for the
circumference of a circle is 2πr.
In gross general terms: Expressions produce at least one value.
In Python, expressions are covered extensively in the Python Language Reference In general, expressions in Python are composed of a syntactically legal combination of Atoms, Primaries and Operators.
Python expressions from Wikipedia
Examples of expressions:
Literals and syntactically correct combinations with Operators and built-in functions or the call of a user-written functions:
>>> 23
23
>>> 23l
23L
>>> range(4)
[0, 1, 2, 3]
>>> 2L*bin(2)
'0b100b10'
>>> def func(a): # Statement, just part of the example...
... return a*a # Statement...
...
>>> func(3)*4
36
>>> func(5) is func(a=5)
True
Statement from Wikipedia:
In computer programming a statement
can be thought of as the smallest
standalone element of an imperative
programming language. A program is
formed by a sequence of one or more
statements. A statement will have
internal components (e.g.,
expressions).
Python statements from Wikipedia
In gross general terms: Statements Do Something and are often composed of expressions (or other statements)
The Python Language Reference covers Simple Statements and Compound Statements extensively.
The distinction of "Statements do something" and "expressions produce a value" distinction can become blurry however:
List Comprehensions are considered "Expressions" but they have looping constructs and therfore also Do Something.
The if is usually a statement, such as if x<0: x=0 but you can also have a conditional expression like x=0 if x<0 else 1 that are expressions. In other languages, like C, this form is called an operator like this x=x<0?0:1;
You can write you own Expressions by writing a function. def func(a): return a*a is an expression when used but made up of statements when defined.
An expression that returns None is a procedure in Python: def proc(): pass Syntactically, you can use proc() as an expression, but that is probably a bug...
Python is a bit more strict than say C is on the differences between an Expression and Statement. In C, any expression is a legal statement. You can have func(x=2); Is that an Expression or Statement? (Answer: Expression used as a Statement with a side-effect.) The assignment statement of x=2 inside of the function call of func(x=2) in Python sets the named argument a to 2 only in the call to func and is more limited than the C example.
Though this isn't related to Python:
An expression evaluates to a value.
A statement does something.
>>> x + 2 # an expression
>>> x = 1 # a statement
>>> y = x + 1 # a statement
>>> print y # a statement (in 2.x)
2
An expression is something that can be reduced to a value, for example "1+3" is an expression, but "foo = 1+3" is not.
It's easy to check:
print(foo = 1+3)
If it doesn't work, it's a statement, if it does, it's an expression.
Another statement could be:
class Foo(Bar): pass
as it cannot be reduced to a value.
Statements represent an action or command e.g print statements, assignment statements.
print 'hello', x = 1
Expression is a combination of variables, operations and values that yields a result value.
5 * 5 # yields 25
Lastly, expression statements
print 5*5
An expression is something, while a statement does something.
An expression is a statement as well, but it must have a return.
>>> 2 * 2         #expression
>>> print(2 * 2)     #statement
PS:The interpreter always prints out the values of all expressions.
An expression is a statement that returns a value. So if it can appear on the right side of an assignment, or as a parameter to a method call, it is an expression.
Some code can be both an expression or a statement, depending on the context. The language may have a means to differentiate between the two when they are ambiguous.
STATEMENT:
A Statement is a action or a command that does something. Ex: If-Else,Loops..etc
val a: Int = 5
If(a>5) print("Hey!") else print("Hi!")
EXPRESSION:
A Expression is a combination of values, operators and literals which yields something.
val a: Int = 5 + 5 #yields 10
Expressions always evaluate to a value, statements don't.
e.g.
variable declaration and assignment are statements because they do not return a value
const list = [1,2,3];
Here we have two operands - a variable 'sum' on the left and an expression on the right.
The whole thing is a statement, but the bit on the right is an expression as that piece of code returns a value.
const sum = list.reduce((a, b)=> a+ b, 0);
Function calls, arithmetic and boolean operations are good examples of expressions.
Expressions are often part of a statement.
The distinction between the two is often required to indicate whether we require a pice of code to return a value.
References
Expressions and statements
2.3 Expressions and statements - thinkpython2 by Allen B. Downey
2.10. Statements and Expressions - How to Think like a Computer Scientist by Paul Resnick and Brad Miller
An expression is a combination of values, variables, and operators. A value all by itself is
considered an expression, and so is a variable, so the following are all legal expressions:
>>> 42
42
>>> n
17
>>> n + 25
42
When you type an expression at the prompt, the interpreter evaluates it, which means that
it finds the value of the expression. In this example, n has the value 17 and n + 25 has the
value 42.
A statement is a unit of code that has an effect, like creating a variable or displaying a
value.
>>> n = 17
>>> print(n)
The first line is an assignment statement that gives a value to n. The second line is a print
statement that displays the value of n.
When you type a statement, the interpreter executes it, which means that it does whatever
the statement says. In general, statements don’t have values.
An expression translates to a value.
A statement consumes a value* to produce a result**.
*That includes an empty value, like: print() or pop().
**This result can be any action that changes something; e.g. changes the memory ( x = 1) or changes something on the screen ( print("x") ).
A few notes:
Since a statement can return a result, it can be part of an expression.
An expression can be part of another expression.
Statements before could change the state of our Python program: create or update variables, define function, etc.
And expressions just return some value can't change the global state or local state in a function.
But now we got :=, it's an alien!
Expressions:
Expressions are formed by combining objects and operators.
An expression has a value, which has a type.
Syntax for a simple expression:<object><operator><object>
2.0 + 3 is an expression which evaluates to 5.0 and has a type float associated with it.
Statements
Statements are composed of expression(s). It can span multiple lines.
A statement contains a keyword.
An expression does not contain a keyword.
print "hello" is statement, because print is a keyword.
"hello" is an expression, but list compression is against this.
The following is an expression statement, and it is true without list comprehension:
(x*2 for x in range(10))
Python calls expressions "expression statements", so the question is perhaps not fully formed.
A statement consists of pretty much anything you can do in Python: calculating a value, assigning a value, deleting a variable, printing a value, returning from a function, raising an exception, etc. The full list is here: http://docs.python.org/reference/simple_stmts.html#
An expression statement is limited to calling functions (e.g.,
math.cos(theta)"), operators ( e.g., "2+3"), etc. to produce a value.

Mixing mathematical expression with control flow

I would like to know, how expressions are parsed when are mixed with control flow.
Let's assume such syntax:
case
when a == Method() + 1
then Something(1)
when a == Other() - 2
then 1
else 0
end
We've got here two conditional expressions, Method() + 1, Something(1) and 0. Each can be translated to postfix by Shunting-yard algorithm and then easily translated into AST. But is it possible to extend this algorithm to handle control - flow also? Or are there other approaches to solve such mixing of expressions and control flows?
another example:
a == b ? 1 : 2
also how can I classify such expression: a between b and c, can I say that between is three arguments function? Or is there any special name for such expressions?
You can certainly parse the ternary operator with an operator-precedence grammar. In
expr ? expr : expr
the binary "operator" here is ? expr :, which conveniently starts and ends with an operator token (albeit different ones). To adapt shunting yard to that, assign the right-precedence of ? and the left-precedence of : to the precedence of the ?: operator. The left-precedence of ? and the right-precedence of : are ±∞, just like parentheses (which, in effect, they are).
Since the case statement is basically repeated application of the ternary operator, using slightly different spellings for the tokens, and yields to a similar solution. (Here case when and end are purely parenthetic, while then and the remaining whens correspond to ? and :.)
Having said that, it really is simpler to use an LALR(1) parser generator, and there is almost certainly one available for whatever language you are writing in.
It's clear that both the ternary operator and OP's case statement are operator grammars:
Ternary operator:
ternary-expr: non-ternary-expr
| non-ternary-expr '?' expr ':' ternary-expr
Normally, the ternary operator will be lower precedence from any other operator and associate to the right, which is how the above is written. In C and other languages ternary expressions have the same precedence as assignment expressions, which is straightforward to add. That results in the relationships
X ·> ?
? <· X
? ·=· :
X ·> :
: <· X
Case statement (one of many possible formulations):
case_statement: 'case' case_body 'else' expr 'end'
case_body: 'when' expr 'then' expr
| case_body 'when' expr 'then' expr
Here are the precedence relationships for the above grammar:
case <· when
case ·=· else
when <· X (see below)
when ·=· then
then ·> when
then ·> else
else <· X
else ·=· end
X ·> then
X ·> when
X ·> end
X in the above relations refers to any binary or unary operator, any value terminal, ( and ).
It's straightforward to find left- and right-precedence functions for all of those terminals; the pattern will be similar to that of parentheses in a standard algebraic grammar.
The Shunting-yard algorithm is for expressions with unary and binary operators. You need something more powerful such as LL(1) or LALR(1) to parse control flow statements, and once you have that it will also handle expressions as well. No need for the Shunting-yard algorithm at all.

Nested case statements in SML

This is more of a stylistic question than anything else. Given the following piece of code:
case e1 of (* datatype type_of_e1 = p1 | p2 *)
p1 => case e11 of (* datatype type_of_e11 = NONE | SOME int *)
NONE => expr11
| SOME v => expr12 v
| p2 => case e21 of (* datatype type_of_e21 = NONE | SOME string *)
NONE => expr21
| SOME v => expr22 v
Is there a way to resolve the types of rules don't agree error caused by trying to pattern match e11 to p2, other than enclosing p1's expression in parenthesis? The p2 pattern has another case statement, to avoid the 'just switch the patterns' answer ;-).
update: changed the code to reflect a more concrete case
The answer is "(" and ")". My example:
case e1 of
p1 => ( case e11 of
NONE => expr11
| SOME v => expr12 v )
| p2 => ( case e21 of
NONE => expr21
| SOME v => expr22 v )
This really works! Cool :) You can try it too.
No. The syntactic rules in the Definition of Standard ML state that the match arms of a case expression attempt to maximally consume potential clauses. And since there's no "end case" or similar marker in the language, the parser will merrily eat each of the "| pat => exp" clauses that you feed it until it sees something that terminates a list of match clauses.
Plain and short answer: no. But what's wrong with parentheses?
(Of course, you can also bracket in other ways, e.g. with a 'let', or by factoring into auxiliary functions, but parentheses are the canonical solution.)

Not equal/not contains operation in lambda expressions

My query is some thing like:
var subQuery = contacts_requests.Where(i => i.requests_usr_id >= 1).Select
(i => i.Usr_contacts_requests_from_usr_id ).ToArray();
var query = biographic_details.Join(profiles_companies, i => i.usr_id, j => j.company_usr_id,
(i,j)=>new{
Usr_bio_usr_id }).where(p=>subQuery.Contains(i.company_usr_id)).ToArray();
I want notcontains operation in place of contains, how can I implement that?
Instead of
p => subQuery.Contains(i.company_usr_id)
use
p => !subQuery.Contains(i.company_usr_id)
Note the ! before the method call. The ! operator (aka Logical negation operator) just negates the result of the following expression. So Contains becomes Not Contains.
To achieve not contains you can use !subQuery.Contains(i.company_usr_id).

How to define more one condition in a While

I would like something like that :
While Not RdoRst.EOF And RdoRst(2) = "Foo"
cboComboBox.AddItem RdoRst(1)
cboComboBox.ItemData(cboComboBox.NewIndex) = RdoRst(0)
RdoRst.MoveNext
Wend
I want that the expression 1 (Not RdoRst.EOF) is evaluated first. Then if it returns true, the expression 2 is evaluated too (RdoRst(2) = "Foo"). If expression 1 return false, the expression 2 is not evaluated.
Regards,
Florian
AndAlso is not available in VB6. Try this
Do
If RdoRst.EOF Then Exit Do
If Not RdoRst(2) ="Foo" Then Exit Do
cboComboBox.AddItem RdoRst(1)
cboComboBox.ItemData(cboComboBox.NewIndex) = RdoRst(0)
RdoRst.MoveNext
Loop
The question relates to 'short circuit' evaluation of condition expressions. Well VB6 does not support this feature. I know this is stupid.
While Not RdoRst.EOF
If RdoRst(2) = "Foo" Then
cboComboBox.AddItem RdoRst(1)
cboComboBox.ItemData(cboComboBox.NewIndex) = RdoRst(0)
Else
Exit Wend
End If
RdoRst.MoveNext
Wend

Resources