|
From: | Matt Wette |
Subject: | Re: Q on (language <lang> spec) |
Date: | Fri, 23 Oct 2015 06:10:51 -0700 |
I didn’t describe this very well. I will try again. The code actually provides a lexical analyzer (aka lexer) generator-generator. To make a lexer you call make-lexer-generator with a match-table as argument: (define gen-lexer (make-lexer-generator match-table)) Then when you pass a generated lexer each time you call the parser: (parse (gen-lexer)) The reason is that the lexer keeps state information (e.g., the beginning-of-line condition). Now the match table argument indicates how the user wants lexemes, read from the input, to be reported to the parser. If you want “while” in the input to be reported as ‘WHILE to the parser, then the match table would include an entry ‘(“while” . WHILE). The generator uses special symbols to represent quoted strings, numbers and comments. If you want quoted strings returned with the symbol ‘STRING, then the match table would include an entry ‘($string . STRING). In many cases I have nyacc "hashify” my parser so that it uses integers instead of symbols. Here is the match table generated for the hashified matlab parser: (define mtab '(($lone-comm . 1) ($string . 2) ($float . 3) ($fixed . 4) ($ident . 5) ( ";" . 6) (".'" . 7) ("'" . 8) ("~" . 9) (".^" . 10) (".\\" . 11) ("./" . 12) (".*" . 13) ("^" . 14) ("\\" . 15) ("/" . 16) ("*" . 17) ("-" . 18) ( "+" . 19) (">=" . 20) ("<=" . 21) (">" . 22) ("<" . 23) ("~=" . 24) ("==" . 25) ("&" . 26) ("|" . 27) (":" . 28) ("case" . 29) ("elseif" . 30) ( "clear" . 31) ("global" . 32) ("return" . 33) ("otherwise" . 34) ("switch" . 35) ("else" . 36) ("if" . 37) ("while" . 38) ("for" . 39) ("," . 40) ( ")" . 41) ("(" . 42) ("=" . 43) ("]" . 44) ("[" . 45) ("function" . 46) ( #\newline . 47) ("end" . 48) ($end . 49))) and here is the match table generated for the non-hashified match table for the same language: (define mtab '(($lone-comm . $lone-comm) ($string . $string) ($float . $float) ($fixed . $fixed) ($ident . $ident) (";" . #{$:;}#) (".'" . $:.') ("'" . $:') ("~" . $:~) (".^" . $:.^) (".\\" . $:.\) ("./" . $:./) (".*" . $:.*) ("^" . $:^) ("\\" . $:\) ("/" . $:/) ("*" . $:*) ("-" . $:-) ("+" . $:+) (">=" . $:>=) ("<=" . $:<=) (">" . $:>) ("<" . $:<) ("~=" . $:~=) ("==" . $:==) ( "&" . $:&) ("|" . $:|) (":" . $::) ("case" . $:case) ("elseif" . $:elseif) ("clear" . $:clear) ("global" . $:global) ("return" . $:return) ( "otherwise" . $:otherwise) ("switch" . $:switch) ("else" . $:else) ("if" . $:if) ("while" . $:while) ("for" . $:for) ("," . $:,) (")" . #{$:\x29;}# ) ("(" . #{$:\x28;}#) ("=" . $:=) ("]" . #{$:\x5d;}#) ("[" . #{$:\x5b;}#) ("function" . $:function) (#\newline . #\newline) ("end" . $:end) ($end . $end))) |
[Prev in Thread] | Current Thread | [Next in Thread] |