Skip to Content.
Sympa Menu

k-user - Re: [K-user] parsing error?

k-user AT lists.cs.illinois.edu

Subject: K-user mailing list

List archive

Re: [K-user] parsing error?


Chronological Thread 
  • From: Daniele Filaretti <dfilaretti AT gmail.com>
  • To: Radu Mereuta <headness13 AT gmail.com>
  • Cc: "k-user AT cs.uiuc.edu" <k-user AT cs.uiuc.edu>
  • Subject: Re: [K-user] parsing error?
  • Date: Fri, 8 Feb 2013 14:53:40 +0000
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/k-user/>
  • List-id: <k-user.cs.uiuc.edu>

On 7 Feb 2013, at 16:27, Radu Mereuta <headness13 AT gmail.com> wrote:

Regarding that escaping thing... that's a bit tricky. We are still thinking how to do that.

You could try a trick for now. You can declare two productions and say that they have the same label. In the backend they will overlap and provide the same functionality. So you can do something like this:
module TEST-SYNTAX
  syntax Exp ::= Exp "=>" Exp [onlyLabel, klabel('rewrite)] // onlyLabel says that this production will only be used in programs
endmodule

module TEST
  syntax Exp ::= Exp "==>" Exp [klabel('rewrite)] // synchronized with the one above
  rule A ==> B => A + B // in rule you use the production above
endmodule

The parser for programs will use only what is defined in TEST-SYNTAX (and included modules). This way you can create a clean parser for your language. But because it will produce ambiguities in the definition, you can rename the operation for the rule parser by redeclaring it in the TEST module.

This is what we currently use, and we are passively looking for improvements.

Thanks Radu, 
I'll give it a try!

By the way, another (very) temporary solution could be to use sed (unix command) in order to preprocess the source file. For example I was using it in order to transform for example all the "=>" in my source into "==>", and other small things (e.g. if I declare a variable called "$array" the parser will reject it because "array" is a name I use somewhere else -- in that case I replace "$array" with "$array1".
I know it's not very clean but can be useful to quickly test  small examples without having to manually change the file.

Cheers, 
Daniele


On Thu, Feb 7, 2013 at 12:09 PM, Daniele Filaretti <dfilaretti AT gmail.com> wrote:
On 6 Feb 2013, at 19:11, Radu Mereuta <headness13 AT gmail.com> wrote:

Now an explanation for Daniele:

You are right, you don't need a relation between 'fcall' and 'array' because they cannot cause ambiguities. Problems start to appear when you have combinations with binary operators. Example:

syntax Exp ::= Exp "+" Exp | Exp "[" Exp "]"
An input of the form: "a + b [ c ]" is ambiguous because it can be parsed in two ways:  (a + b) [ c ] and  a + (b [ c ])

A priority filter in SDF (we implemented a similar system in SDF) is a simple function that traverses the parse forest and rejects erroneous branches. For the example above we will have:
amb( _[_](_+_(a, b), c),
        _+_(a, _[_](b, c)))

To solve this you can add in K a relation like this:
syntax priorities _[_] > _+_ // or 'syntax priorities array > plus' if you tag your productions with array and plus.
The filter will look at that relation and whenever finds plus as a parent of array, will reject that branch.

What was happening in your example was that you had a relation between two productions that would reject the tree that you actually wanted.
In your example you could use the verbose way of specifying precedences using tags. Remember though that the priority relation is transitively closed.

Radu, thanks for your explanation, I think I got the point. 

Actually, I find quite hard to manage all of the precedences. For example, I initially just added the obvious ones like multi > add etc, using the 
syntax 
 
   …
   | stuff
   | mult
   > add
   | sub 
   | stuff 
   ….

and then I started adding new productions (e.g. the ones for array etc.), some at the top, some at the bottom… That's how I eventually discovered to have unwanted precedences.

I'll try to do as you said (using "syntax priorities …"), it should be better. 

Also we have some problems with the documentation. I think you would have had a much better time if we would have explained in details the way the parser works. We are also working on a manual.

I agree. The tutorial and the examples are an excellent resource for learning K, but eventually experienced users would benefit more from a proper user manual, so that they can just look for a particular information or command.

Meanwhile feel free to ask us anything you don't find clear. We take users feedback very serious. Many of the tools features have been requested and shaped by users feedback.

Well, this is not related to the previous question: any idea on how to escape sequences in K? More in particular, any idea on how to escape "=>"? 
It is used in my language (PHP) to define arrays (that are really kind of dictionaries, not arrays in the traditional sense): 

$x = ["foo" => 1, 
          4 => 0]

I'm currently using "==>" instead of "=>" but would be nice to fix it. 

Anyway, thanks as usually for the quick and helpful answers! :)

Daniele





On Wed, Feb 6, 2013 at 8:38 PM, Rosu, Grigore <grosu AT illinois.edu> wrote:

Don't worry, Daniele, parsing confuses us all.

 

However, your example raises an interesting point: we should probably use the filters only when we have ambiguities, not all the times.  I think Radu is just using SDF out-of-the-box, so if SDF applies the filters all the time then so does K.  Radu, would it be posssible to change the way we apply the ambiguity filters?

 
Grigore
 
 

From: k-user-bounces AT cs.uiuc.edu [k-user-bounces AT cs.uiuc.edu] on behalf of Daniele Filaretti [dfilaretti AT gmail.com]
Sent: Wednesday, February 06, 2013 12:34 PM
To: Radu Mereuta
Cc: k-user AT cs.uiuc.edu
Subject: Re: [K-user] parsing error?

On 6 Feb 2013, at 18:19, Radu Mereuta <headness13 AT gmail.com> wrote:

Are you sure you don't have a priority relation between fcall and array? That might reject the correct parsing tree, and would explain why it works with [bracket].

Oh, well, in fact I had a precedence (I forgot to mention in my question). 
It was 
Exp[Exp] > Id (Exps)

And yes, if I remove the precedence it works. 
To be honest I don't understand why, parsing issues always confuse me :) 
(I mean, my _expression_ -- foo()[0] is an Exp[Exp]. And Exp[Exp] has the precedence. So there should not be any ambiguity I guess?)

Anyway, thanks a lot! 

Cheers, 
Daniele

On Wed, Feb 6, 2013 at 8:06 PM, Daniele Filaretti <dfilaretti AT gmail.com> wrote:
Hi all,
here is another question for you K hackers!

Let say my grammar contains something that looks like

> syntax Exp ::=
>    …
>    | Id "(" Exps ")" // function call
>    | Exp "[" Exp "]"  [seqstrict] // reading a field from an array (the first Exp must evaluate to an array value, the second to a key value)
>    …

and suppose I write a function, "foo()" that returns an array. Now, if in my program I write something like

> foo()[0];

I get a parsing error:

> [Error] Critical: Parse error: Syntax error near unexpected character ';'
>       File: test.php
>       Location: (5,9,5,9)


As an experiment, I tried to add

> syntax Exp ::= "(" Exp ")" [bracket]

and then, if I evaluate

> (foo())[0]:


it works!

Am I missing something or is a parsing problem?

Thanks a lot!

Cheers,
Daniele


_______________________________________________
k-user mailing list
k-user AT cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/k-user









Archive powered by MHonArc 2.6.16.

Top of Page