k-user AT lists.cs.illinois.edu
Subject: K-user mailing list
List archive
- From: Radu Mereuta <headness13 AT gmail.com>
- To: Abdul Dakkak <abduld AT wolfram.com>
- Cc: k-user AT cs.uiuc.edu
- Subject: Re: [K-user] K Syntax Usage
- Date: Wed, 26 Sep 2012 11:29:52 +0300
- List-archive: <http://lists.cs.uiuc.edu/pipermail/k-user/>
- List-id: <k-user.cs.uiuc.edu>
Hi,
I assume that if fails because it gives an ambiguity warning when parsing the program.
Right now we rely on SDF to do our parsing, so we require to write your grammar in the SDF way. For your first example, SDF doesn't know how to disambiguate because you didn't specify a priority relation between _=_ and _+_ so at runtime we just choose one of them and give a warning (probably it chose the wrong one).
In the second example you got it right although I recommend something like this if you want to simulate the normal priority between operands:
syntax Expr ::= Value
| "(" Expr ")" [bracket]
| AExpr
| left:
| "(" Expr ")" [bracket]
| AExpr
| left:
Expr "/" Expr [left, strict]
| Expr "*" Expr [left, strict]
> left:
| Expr "*" Expr [left, strict]
> left:
Expr "+" Expr [left, strict]
| Expr "-" Expr [left, strict]
| Expr "-" Expr [left, strict]
> Identifier "=" Expr [strict(2), right]
/ and * have a higher priority than + and -, but they have the same priority regarding each other, so we say that they are left associative. Also in the square brackets we say that it is left associative with itself. Examples (something with no parenthesis will parse like this):
a = (1 + (2 * 3)), a = ((1 + 2) + 3, ), a = (b = c), a = ((1 + 2) - 3)
Hope this answers your questions.
P.S. The definition does not fail because you don't use '+', '-', '*', '/' or '=' next to each other, so they don't give ambiguities.
Radu Mereuta
Developer on K
On Tue, Sep 25, 2012 at 5:38 PM, Abdul Dakkak <abduld AT wolfram.com> wrote:
I have a simple k file that defines a `MY` language. My question is why the following does not work
module MY-SYNTAX
syntax Integer ::= Int
syntax Literal ::= String
| Integer
| Real
syntax Identifier ::= Id
| ":" Id
syntax Value ::= Identifier
| Literal
syntax AExpr ::=
Expr "+" Expr [left, strict]
| Expr "-" Expr [left, strict]
| Expr "/" Expr [left, strict]
| Expr "*" Expr [left, strict]
syntax Expr ::= Value
| "(" Expr ")" [bracket]
> AExpr
> Identifier "=" Expr [strict(2), right]
endmodule
module MY
imports MY-SYNTAX
syntax KResult ::= Literal
configuration <T color="yellow">
<k color="green"> $PGM:K </k>
<env color="LightSkyblue"> .Map </env>
<store color="red"> .Map </store>
</T>
rule <k>X:Identifier = E => . ...</k>
<env> ... Rho => Rho[N/X] </env>
<store> ... . => (N |-> E) ... </store>
when fresh(N:Int)
rule I1:Int + I2:Int => I1 +Int I2
endmodule
If I inline the AExpr then it works fine
module MY-SYNTAX
syntax Integer ::= Int
syntax Literal ::= String
| Integer
| Real
syntax Identifier ::= Id
| ":" Id
syntax Value ::= Identifier
| Literal
syntax Expr ::= Value
| "(" Expr ")" [bracket]
> AExpr
> Expr "+" Expr [left, strict]
> Expr "-" Expr [left, strict]
> Expr "/" Expr [left, strict]
> Expr "*" Expr [left, strict]
> Identifier "=" Expr [strict(2), right]
endmodule
module MY
imports MY-SYNTAX
syntax KResult ::= Literal
configuration <T color="yellow">
<k color="green"> $PGM:K </k>
<env color="LightSkyblue"> .Map </env>
<store color="red"> .Map </store>
</T>
rule <k>X:Identifier = E => . ...</k>
<env> ... Rho => Rho[N/X] </env>
<store> ... . => (N |-> E) ... </store>
when fresh(N:Int)
rule I1:Int + I2:Int => I1 +Int I2
endmodule
the example does not fail on kompile, but on krun with this simple program
g = 1 + 2
any insight is appreciated.
-adk-
_______________________________________________
k-user mailing list
k-user AT cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/k-user
- [K-user] K Syntax Usage, Abdul Dakkak, 09/25/2012
- Re: [K-user] K Syntax Usage, Radu Mereuta, 09/26/2012
Archive powered by MHonArc 2.6.16.