Skip to Content.
Sympa Menu

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

k-user AT lists.cs.illinois.edu

Subject: K-user mailing list

List archive

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


Chronological Thread 
  • From: Ulrich Kühne <ulrichk AT informatik.uni-bremen.de>
  • To: "k-user AT cs.uiuc.edu" <k-user AT cs.uiuc.edu>
  • Subject: [K-user] How to give meaning to newline or tab
  • Date: Wed, 24 Jul 2013 17:41:37 +0200
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/k-user/>
  • List-id: <k-user.cs.uiuc.edu>

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





Archive powered by MHonArc 2.6.16.

Top of Page