javacc笔记

WHAT IS LOOKAHEAD?

The job of a parser is to read an input stream and determine whether or not the input stream conforms to the grammar.




LOOKAHEAD(2)

Suppose you set the value of this option to 2. Then the LOOKAHEAD algorithm derived from this looks at two tokens (instead of just one token) before making a choice decision


void basic_expr() :
	{}
	{
	  LOOKAHEAD(2)
	  <ID> "(" expr() ")"	// Choice 1
	|
	  "(" expr() ")"	// Choice 2
	|
	  "new" <ID>		// Choice 3
	|
	  <ID> "." <ID>		// Choice 4
	}

翻译过来:


	if (next 2 tokens are <ID> and "(" ) {
	  choose Choice 1
	} else if (next token is "(") {
	  choose Choice 2
	} else if (next token is "new") {
	  choose Choice 3
	} else if (next token is <ID>) {
	  choose Choice 4
	} else {
	  produce an error message
	}

你可能感兴趣的:(javacc笔记)