[antlr-interest] Re: need help writing a source code formatter

Adrian Sandor aditsu at yahoo.com
Fri Jun 27 01:22:40 PDT 2003


--- In antlr-interest at yahoogroups.com, "Adrian Sandor" <aditsu at y...> 
wrote:
> - could somebody have some mercy and write a simple example, like 
> parsing a string of correctly matched parentheses with spaces
> between them and stripping all spaces and adding a '+' after
> each '(' and a '-' before each ')' ?
> e.g. "( ()( (  ))  )" -> "(+(+-)(+(+-)-)-)"

looks like I managed to write this example by myself:

//-----snip-----
class par_p extends Parser;
{
	private void wr(String x)
	{
		System.out.print(x);
	}
	private void disp(Token x)
	{
		wr(x.getText());
	}
}

s: ws (par1 ws)*; //start
par1: o:OP{disp(o);wr("+");} s c:CL{wr("-");disp(c);}; //one pair of 
parentheses
ws: (SP)*; //whitespace

class par_l extends Lexer;

OP: '(';
CL: ')';
SP: ' ';
//-----snip-----

it works well without warnings, but if it can be improved then please 
comment on it
now I'll try to restart writing the C++ grammar, without 
nondeterminisms and ambiguities

Adrian


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list