Skip to Content.
Sympa Menu

k-user - Re: [K-user] How to give meaning to newline or tab

k-user AT lists.cs.illinois.edu

Subject: K-user mailing list

List archive

Re: [K-user] How to give meaning to newline or tab


Chronological Thread 
  • From: "Guth, Dwight" <dguth2 AT illinois.edu>
  • To: Ulrich Kühne <ulrichk AT informatik.uni-bremen.de>, "k-user AT cs.uiuc.edu" <k-user AT cs.uiuc.edu>
  • Subject: Re: [K-user] How to give meaning to newline or tab
  • Date: Wed, 24 Jul 2013 17:19:55 +0000
  • Accept-language: en-US
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/k-user/>
  • List-id: <k-user.cs.uiuc.edu>

Hi,

Unfortunately as far as I am aware what you are trying to do is not possible
in the K framework at this time. The issue is that in order to make the vast
majority of languages work it is necessary to define the meaning of
whitespace in your parser. Currently we define whitespace as spaces, tabs,
and newlines, and instruct the parser for programs to treat them as layout
characters, which means they do not turn into tokens but are allowed to
separate tokens by appearing in between any two production items (i.e.
terminals and nonterminals) in any place in the rest of the grammar. This
causes the newlines you are trying to give a semantic meaning to to disappear
before the parser attempts to process them, and so it finds an "x" when it is
looking for a "\n".

As you note, this is a feature similar to the python language, and so I fully
expect we will eventually support some kind of mechanism to define the syntax
of whitespace and comments in any way we choose, rather than forcing them to
obey the default conventions provided by the K framework. We will let users
know when such a change has been made. However, in the meantime, if you
really want to use newlines as statement separators in your language, you
will have to do as I did in python and use an external parser.

Dwight
K and Python semantics developer
________________________________________
From:
k-user-bounces AT cs.uiuc.edu

[k-user-bounces AT cs.uiuc.edu]
on behalf of Ulrich Kühne
[ulrichk AT informatik.uni-bremen.de]
Sent: Wednesday, July 24, 2013 10:41 AM
To:
k-user AT cs.uiuc.edu
Subject: [K-user] How to give meaning to newline or tab

Hi,

I would like to give some meaning to the newline character '\n' like it
is used in python, as a delimiter. Checking the examples in the K
distribution, I think I know how to define the lexing rule. However, the
K parser obviously has its own idea of what to do with newlines, so my
definition does not work. Is there any way to use newline (or tab stops)
as tokens without using an external parser?

I also looked into the k-python project, they are actually using an
external parser, but I would very much like to avoid having to generate
these crazy ASCII noise ASTs ;-)

Here is my first try for a K module using newlines (instead of semicolons):

module NEWLINE-SYNTAX
syntax Newline ::= Lexer{[\n]+} // use newline as a delimiter
syntax Newline -/- [\n] // make it a maximum match

syntax Exp ::= Int | Int "+" Int [strict,left]

syntax Stmt ::= "var" Id
| Id "=" Exp [strict(2)]
| Exp [avoid]
syntax Stmts ::= Stmt | Stmts Newline Stmts [right]
endmodule

module NEWLINE imports NEWLINE-SYNTAX
syntax Val ::= Int
syntax KResult ::= Val

// heat up statements
rule <k>S:Stmt Newline Rest => S ~> Rest</k> [structural]
endmodule


Running krun on a sample file leads to a syntax error, since the
newlines have disappeared:

>$ cat test
var x
x = 1 + 2

>$ krun test
[Error] Critical: Parse error: Syntax error near unexpected character 'x'
File: test
Location: (2,1,2,1)

Any help is appreciated.

regards,
Ulrich

_______________________________________________
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