






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The role of a scanner and parser in designing a formula parser during the cs 1530 software engineering fall 2004 course. The scanner is responsible for converting the character stream into logical units (tokens), while the parser creates a parse tree from these tokens, representing the grammatical structure and precedence and associativity rules. The document also covers the use of jlex and java cup for generating scanners and parsers.
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







CS 1530 Software 2004 Engineering Fall
CS 1530 Software Engineering Fall 2004
■ Scanner
■ uses regular expressions that specify the tokens ■ Parser
■ represents the grammatical structure ■ represents precdence and associativity rules ■ uses (context-free) grammar to represent structure ■ Can be written by hand
CS 1530 Software 2004 Engineering Fall
Scanner (aka lexical analyzer) symbol table (formula)^ source^ parser token get next token Scanning = converting character stream into logical units (aka. tokens)
CS 1530 Software 2004 Engineering Fall
lexical analyzer symbol table source parser token get next token parse tree Rest of spreadsheet
CS 1530 Software Engineering Fall 2004
CS 1530 Software 2004 Engineering Fall
public class P { public static void main(String[] FileReader inFile = new FileReader args) {(args[0]); Yylex scanner = new Yylex(inFile); while (token.sym != sym.EOF) {^ Symbol token = scanner.next_token(); switch (token.sym) { case sym.INTLITERAL: System.out.println("INTLITERAL ("
CS 1530 Software 2004 Engineering Fall
■ Closely follow standard conventions.
■ abc ■ == ■ while
■ “a|b” matches a|b not a or b ■ “a\”\”\tb” matches a””\tb not a””
CS 1530 Software 2004 Engineering Fall
CS 1530 Software 2004 Engineering Fall
■ ^ matches beginning of line
■ $ matches end of line
CS 1530 Software Engineering Fall 2004
■ [abc] ■ matches one character (either a or b or c) ■ [a-z] ■ matches any character between a and z, inclusive ■ [^abc] ■ matches any character except a, b, or c. ■ ^ has special meaning only at 1 st^ position in […] ■ [\t\] ■ matches tab or
■ [a bc] is equivalent to a|" "|b|c ■ white-space in char class and strings matches itself CS 1530 Software 2004 Engineering Fall
■ specified in the second part of xxx.jlex. ■ can also specify (see the manual for details) ■ the value to be returned on end-of-file, ■ that line counting should be turned on, and ■ that the scanner will be used with the parser generator java cup. ■ directives includes macro definitions (very useful): ■ ■ name is any valid Java identifier ■ DIGIT= [0-9] ■ LETTER= [a-zA-Z] ■ WHITESPACE= [ \t\n] ■ To use a macro, use its name inside curly braces. ■ {LETTER}({LETTER}|{DIGIT})*
CS 1530 Software 2004 Engineering Fall
int val = (new Integer(yytext())).intValue(); Symbol S = new Symbol(sym.INTLITERAL, new IntLitTokenVal(yyline+1, CharNum.num, val)); CharNum.num += yytext().length(); return S; } {WHITESPACE}+ {CharNum.num += yytext().length();} CS 1530 Software 2004 Engineering Fall
CS 1530 Software 2004 Engineering Fall
■ a parser generator
■ Java successor to yacc
java Java_CUP.Main < xxx.cup
parser specification
CS 1530 Software 2004 Engineering Fall
■ optional package and import declarations ■ optional user code ■ terminal and nonterminal declarations ■ optional precedence and associativity declarations ■ grammar rules with associated actions CS 1530 Software Engineering Fall 2004
FileReader inFile = new FileReader(args[0]); parser P = new parser(new Yylex(inFile)); Symbol root=null; // the parser will return a Symbol whose value // field's type is the type associated with the // root nonterminal (i.e., with the nonterminal // "program") root = P.parse(); // do the parse CS 1530 Software 2004 Engineering Fall
■ all terminals and nonterminals used by your grammar must be declared terminal type name1, name2, ... ; non terminal type name1, name2, ... ; non terminal name1, name2, ... ;
■ type = the type of Symbol. value returned by scanner
CS 1530 Software 2004 Engineering Fall