RE: Ignore break line sometimes

"Karsten Nyblad" <>
Sun, 12 Feb 2012 09:21:48 +0100

          From comp.compilers

Related articles
Ignore break line sometimes (Geovani de Souza) (2012-02-11)
RE: Ignore break line sometimes (Karsten Nyblad) (2012-02-12)
Re: Ignore break line sometimes (Kaz Kylheku) (2012-02-13)
| List of all articles for this month |

From: "Karsten Nyblad" <>
Newsgroups: comp.compilers
Date: Sun, 12 Feb 2012 09:21:48 +0100
Organization: Compilers Central
References: 12-02-010
Keywords: parse
Posted-Date: 12 Feb 2012 16:14:24 EST

> I'm trying write an parser to my compiler, and I'm interessed to
ignore the break line (\n) sometimes. E.g:
> if true then [\n]
> foo(); [\n]
> end; [\n]

One option is to write a recursive descendent parser, and have two ways
of calling the lexer: One that return line ends and one that does not.

An other option is to base your parsing on a parser generator like
bison, and modify the code that drives the automaton. That code is
modified such that when the lexer returns a line feed token, you copy
the stack of states, and on the copy you simulate the actions that the
parser would have taken. When the simulation stacks the line feed, you
throw away the copy and resume parsing on the real stack with the line
feed in the window. When the simulation encounters an error, you throw
away the simulation AND the line feed and call the lexer again.

If you chose the second option, it is important that you chose the right
parser generator, because some parser generators already generate code
that can help you. Many LR parser generators, e.g., bison, include
facilities for generalised LR parsing, and many LL parser generators
include facilities for backtracking. That might help you.

Karsten Nyblad

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.