Top  Previous  Next

Scripts > Class elements and c++ instructions > Calling a production > Look-ahead


Productions can be invoked in the interpreter to test whether the input text matches the production at the current position. If you use the look-ahead in combination with the IF symbol or by WHILE, you can parse texts, which aren't LL(1) conform or which are difficult to describe with a LL(1) grammar.

By this look-ahead the current position isn't changed and the semantic code of the production isn't executed. Parameters therefore aren't necessary. The TextTransformer just recognizes a look-ahead call by the missing parameter, opposed to the call of a sub parser, which needs at least a string parameter.

The text is tested either until a token is expected which doesn't exist in the text or until the last symbol of the production is recognized. So a bool value will be returned which is true if the text matches the production and false, if it doesn't match. Under no (logical) circumstances exceptions are thrown.

A look-ahead can depend on other look-ahead productions which are tested inside of the first. The different levels of the look-ahead are represented as a number both in a separate field of the tool bar and as a preceding number in the stack window.


The part of text, which was recognized by the look-ahead can be accessed by the   la_str() method of the parser state class. For example, you can pass this string to a sub parser.


A detailed example is the Java parser.



The result of the look-ahead depends on whether the look-ahead production is part of the system where it is called or not.



The production Ident is used as a look-ahead production in the same parser system, where it is used as a normal production. The input "int" yields the expected result: "int found", if the option  for testing all literals is activated.




"int" {{cout << "int found"; }}


Ident  {{cout << "error"; }}



Ident ::= IDENT

IDENT ::= \w+


If, however, an external production is used for the look-ahead:


isIdent ::= IDENT



"int" {{cout << "int found"; }}


IDENT  {{cout << "error"; }}



the input "int" yields the error: "IDENT" expected. Since isIdent is a start production which isn't used in the main parser, it doesn't know the tokens of the main parser either. So "int" is nterpreted as IDENT and !isIdent() is wrong. The ELSE alternative is chosen. However, "int" was recognized in the main system as int what isn't accepted by IDENT then.


In other cases it is to use an external production for looking ahead is of advantage. So in the next example: SKIP will find the character at the end of a sentence even if the sentence starts with "What" or "How".


isQuestion ::=




| ( "." | "!" )




Sentence ::=







Question ::=


   "What" {{ out << "You should better know than I!"; }}

| "How"  {{ out << "I don't know how!"; }}


| {{ out << "I don't understand your question!"; }}

IDENT+ "?"


NonQuestion ::=

IDENT+ ( "." | "!" )


out << "That's interesting!";





This page belongs to the TextTransformer Documentation

Home  Content  German