From: Matt Wette <matthew.wette@verizon.net>
To: guile-user@gnu.org
Subject: Re: Q on (language <lang> spec)
Date: Fri, 23 Oct 2015 06:10:51 -0700 [thread overview]
Message-ID: <7F1E38C4-E065-4858-B190-FB8431DB223A@verizon.net> (raw)
In-Reply-To: <040AE36A-A8DC-4E37-BB91-EF83C1ADBBDB@verizon.net>
[-- Attachment #1: Type: text/plain, Size: 4010 bytes --]
> On Oct 22, 2015, at 5:20 PM, Matt Wette <matthew.wette@verizon.net> wrote:
>
>
>> On Oct 18, 2015, at 8:53 PM, Nala Ginrut <nalaginrut@gmail.com <mailto:nalaginrut@gmail.com>> wrote:
>> And I'm grad that you write a new lexer generator (before it I only know
>> silex), it's great! Would you like to make the generated tokens
>> compatible with scm-lalr? If so, people may rewrite their lexer module
>> with your lexer generator, and no need to rewrite the parser. I saw the
>> token name is string rather than symbol, so I guess it's not compatible
>> with scm-lalr.
>
> Actually, the lexer-generator uses convention of internally turning certain lexemes, like strings, into symbols like ‘$string, or integers into ‘$fixed. The argument to the lexer-generator is a “match-table” which says how to map the read items quoted items are identifiers (e.g., “while”) or character sequences (e.g., “+=“) to something the parser wants to see. For example, if you use the symbol WHILE to denote the source text “while” then you would have an entry (“while” . ‘WHILE) in the match table. So I think the lexer-generator should be adaptable to other parsers.
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)))
[-- Attachment #2: Type: text/html, Size: 11458 bytes --]
prev parent reply other threads:[~2015-10-23 13:10 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-17 19:02 Q on (language <lang> spec) Matt Wette
2015-10-18 20:00 ` Matt Wette
2015-10-19 3:53 ` Nala Ginrut
2015-10-23 0:20 ` Matt Wette
2015-10-23 13:10 ` Matt Wette [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7F1E38C4-E065-4858-B190-FB8431DB223A@verizon.net \
--to=matthew.wette@verizon.net \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).