Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.linkpendium.com!news.linkpendium.com!news.iecc.com!nerds-end From: "Karsten Nyblad" Newsgroups: comp.compilers Subject: RE: Ignore break line sometimes Date: Sun, 12 Feb 2012 09:21:48 +0100 Organization: Compilers Central Lines: 26 Sender: johnl@iecc.com Approved: comp.compilers@iecc.com Message-ID: <12-02-015@comp.compilers> References: <12-02-010@comp.compilers> NNTP-Posting-Host: news.iecc.com X-Trace: leila.iecc.com 1329081264 17812 64.57.183.58 (12 Feb 2012 21:14:24 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Sun, 12 Feb 2012 21:14:24 +0000 (UTC) Keywords: parse Posted-Date: 12 Feb 2012 16:14:24 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: x330-a1.tempe.blueboxinc.net comp.compilers:455 > 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