16.12 A Sling Parser


 
Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  16.   Parsing an Imperative Language

    Content

You can create a Sling parser almost directly from the Sling grammar. You must supplement the parser with the assemblers, as Section 16.11 shows. You also must add logic for reserved words.

16.12.1 Reserved Words

In Sling, you want to allow variable definition, but you do not want to allow confusion about whether pi , s1 , and s2 , for example, are variables or built-in expressions. You do not want to allow

 s1 = 42; // wrong 

In fact, entering this line as a program in the Sling development environment will elicit this message:

 > s1 is a reserved word 

The strategy that SlingParser uses to reserve words is to modify the standard tokenizer to return specified words as a different type of token. The parser uses the class WordOrReservedState developed in Chapter 11, "Extending the Parser Toolkit," to replace the normal word state in the Tokenizer class. The SlingParser class has a tokenizer() method that diverts characters that begin words to a WordOrReservedState object. Most of the subparsers in SlingParser reserve a word from the Sling language by calling SlingParser.reserve() . This method adds a word to the WordOrReservedState object and establishes the sought word as a ReservedLiteral rather than a normal Literal parser. A ReservedLiteral parser matches only a particular word, and it expects the token type to indicate a reserved word.

When the Sling parser tries to match s1 = 42; , the parser fails completely because s1 will tokenize as a reserved word, and nothing in the language allows a reserved word to begin an assignment statement. The Sling mediator checks for this common problem, notes that the parser cannot get past the s1 token, and issues the message that s1 is a reserved word.

16.12.2 Assembler Placement

Figure 16.29 shows where the assemblers plug in. As the first note to the table describes, the noArgs() , oneArg() , and twoArgs() methods need to know both the name of the function to match and a prototype of the function. For example, the baseElement subparser uses the following code to add recognition of a sling function:

 baseElement.add(twoArg("sling", new Sling())); 

The twoArg() method uses the function prototype to plug in the appropriate function assembler:

 protected Parser twoArg(String name, SlingFunction f) {      Track t = new Track(name);     t.add(reserve(name));     t.add(lParen());     t.add(expression());     t.add(comma());     t.add(expression());     t.add(rParen());     t.setAssembler(new FunctionAssembler(f));     return t; } 

With strategies in place for reserved words and function prototypes , you can follow the guidelines in Section 3.6, "Translating a Grammar to Code," to create the parser.


   
Top


Building Parsers with Java
Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net