Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
lfa:lab [2018/10/02 11:02] pdmatei |
lfa:lab [2018/10/02 11:20] (current) pdmatei |
||
|---|---|---|---|
| Line 108: | Line 108: | ||
| <code java> | <code java> | ||
| + | import java.io.*; | ||
| + | import java.util.*; | ||
| + | public class Hello { | ||
| + | public static void main (String[] args) throws IOException { | ||
| + | HelloLexer l = new HelloLexer(new FileReader(args[0])); | ||
| + | |||
| + | l.yylex(); | ||
| + | |||
| + | System.out.println(l.words); | ||
| + | |||
| + | | ||
| + | } | ||
| + | } | ||
| </code> | </code> | ||
| + | * Note that the lexer constructor method receives a java Reader as input (other options are possible, see the docs), and we take the name of the file to-be-scanned from standard input. | ||
| + | * Each lexer implements the method ''yylex'' which starts the scanning process. | ||
| + | |||
| + | After compiling: | ||
| + | <code> | ||
| + | javac HelloLexer.java Hello.java | ||
| + | </code> | ||
| + | |||
| + | and running: | ||
| + | |||
| + | <code> | ||
| + | java Hello | ||
| + | </code> | ||
| + | |||
| + | we obtain: | ||
| + | <code> | ||
| + | |||
| + | |||
| + | |||
| + | 6 | ||
| + | </code> | ||
| + | at standard output. | ||
| + | |||
| + | Recall that the option ''standalone'' tells the lexer to print unmatched words. In our example, those unmatched words are whitespaces. | ||
| ==== Application - parsing lists ==== | ==== Application - parsing lists ==== | ||
| + | |||
| + | Consider the following BNF grammar which describes lists: | ||
| + | <code> | ||
| + | <integer> ::= [0-9]+ | ||
| + | <op> ::= "++" | ":" | ||
| + | <element> ::= <integer> | <op> | <list> | ||
| + | <sequence> ::= <element> | <element> " " <sequence> | ||
| + | <list> ::= | "()" | "(" <sequence> ")" | ||
| + | </code> | ||
| + | |||
| + | The following are examples of lists: | ||
| + | <code> | ||
| + | (1 2 3) | ||
| + | (1 (2 3) 4 ()) | ||
| + | (1 (++ (: 2 (3)) (4 5)) 6) | ||
| + | </code> | ||
| + | |||
| + | Your task is to: | ||
| + | * correctly parse such lists: | ||
| + | * write a JFlex file to implement the lexer: | ||
| + | * Since the language describing lists is Context Free, in order to parse a list, you need to keep track of the opened/closed parenthesis. | ||
| + | * Start by write a PDA (on paper) which accepts correctly-formed lists. Treat each regular expression you defined (for numbers and operators) as a single symbol; | ||
| + | * Implement the PDA (strategy) in the lexer file; | ||
| + | * given a correctly-defined list, write a procedure which evaluates lists operations (in the standard way); For instance, ''(1 (++ (: 2 (3)) (4 5)) 6)'' evaluates to ''(1 (2 3 4 5) 6)'' | ||
| + | * write a procedure which checks if a list is **semantically valid**. What type of checks do you need to implement? | ||