1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--Converted with LaTeX2HTML 98.1p1 release (March 2nd, 1998)
originally by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds
* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>Interfacing with Yacc and Lex</TITLE>
<META NAME="description" CONTENT="Interfacing with Yacc and Lex">
<META NAME="keywords" CONTENT="tpman">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="tpman.css">
<LINK REL="next" HREF="node27.html">
<LINK REL="previous" HREF="node25.html">
<LINK REL="up" HREF="node22.html">
<LINK REL="next" HREF="node27.html">
</HEAD>
<BODY >
<!--Navigation Panel-->
<A NAME="tex2html446"
HREF="node27.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
SRC="/usr/share/latex2html/icons/next.png"></A>
<A NAME="tex2html442"
HREF="node22.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
SRC="/usr/share/latex2html/icons/up.png"></A>
<A NAME="tex2html436"
HREF="node25.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
SRC="/usr/share/latex2html/icons/prev.png"></A>
<A NAME="tex2html444"
HREF="node4.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
SRC="/usr/share/latex2html/icons/contents.png"></A>
<A NAME="tex2html445"
HREF="node58.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index"
SRC="/usr/share/latex2html/icons/index.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html447"
HREF="node27.html">Interfacing with Structure Files</A>
<B> Up:</B> <A NAME="tex2html443"
HREF="node22.html">Running It</A>
<B> Previous:</B> <A NAME="tex2html437"
HREF="node25.html">Using lint</A>
<BR>
<BR>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION00084000000000000000"> </A>
<A NAME="yl-io"> </A>
<BR>
Interfacing with Yacc and Lex
</H2>
The first piece of advice is: try to avoid it, because the SG (SSL) structure
file format is much more convenient.
Notwithstanding that, it is fairly simple, and the term processor handles
a lot of the machinery involved, such as defining a data type for the
<I>yacc</I> stack.
The two important considerations in using <I>yacc</I> are the difference between abstract
syntax and concrete syntax, and handling terminal symbols with values,
such as identifiers.
Abstract syntax should be the input of the term processor and concrete syntax
goes into <I>yacc</I>.
First a simple abstract syntax.
<PRE><HR><!--lgrindfile: yacc_abstract_syntax_example.k 14:54 Jul 22 1996-->
<I>/* Abstract syntax */</I>
funnytree: Str(<B>casestring</B>)
| Cons(funnytree funnytree)
;
<HR></PRE>
<P>
In the <I>yacc</I> input file, concrete syntax is translated into abstract syntax using the
<EM>operator</EM> functions.
Note also the type annotation of concrete symbols, as in <I>%token </I><<I>yt_casestring</I>><I> ID</I>.
This type is a union selector of the generated <I>YYSTYPE</I>.
<A NAME="1043"> </A><A NAME="1044"> </A>
<PRE><HR><!--lgrindfile: yacc_input_example.y 12:44 Oct 26 1992-->
<I>/* Concrete syntax */</I>
%{
<B>#include</B> <code>"k.h"</code>
<BR>
funnytree thetree;
%}
<BR>
%token <yt_casestring> ID
<BR>
%type <yt_funnytree> tree
%%
<BR>
theroot:tree { thetree = $1;};
<BR>
tree: ID { $$ = Str($1);}
| <code>'('</code> tree tree <code>')'</code> { $$ = Cons($2, $3);}
;
<HR></PRE>
<P>
The typical way of using <I>lex</I><A NAME="1440"> </A> is to have the lexical analyser generate tokens
for the parser and build values of the phylum <I>casestring</I>.
The following is the complete <I>lex</I> input.
<A NAME="1050"> </A><A NAME="1051"> </A>
<PRE><HR><!--lgrindfile: lex_input_example.l 15:00 Sep 28 1992-->
<I>/* Lexemes */</I>
%{
<B>#include</B> <code>"k.h"</code>
<B>#include</B> <code>"y.tab.h"</code>
%}
%%
[a-zA-Z0-9]+ { yylval.yt_casestring = mkcasestring(yytext); <B>return</B> ID;}
[\t\n ] { ; } <I>/* skip the white space */</I>
. { <B>return</B> yytext[0]; }
<HR></PRE>
<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html446"
HREF="node27.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
SRC="/usr/share/latex2html/icons/next.png"></A>
<A NAME="tex2html442"
HREF="node22.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
SRC="/usr/share/latex2html/icons/up.png"></A>
<A NAME="tex2html436"
HREF="node25.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
SRC="/usr/share/latex2html/icons/prev.png"></A>
<A NAME="tex2html444"
HREF="node4.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
SRC="/usr/share/latex2html/icons/contents.png"></A>
<A NAME="tex2html445"
HREF="node58.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index"
SRC="/usr/share/latex2html/icons/index.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html447"
HREF="node27.html">Interfacing with Structure Files</A>
<B> Up:</B> <A NAME="tex2html443"
HREF="node22.html">Running It</A>
<B> Previous:</B> <A NAME="tex2html437"
HREF="node25.html">Using lint</A>
<!--End of Navigation Panel-->
<ADDRESS>
<I></I>
<BR><I>2000-04-17</I>
</ADDRESS>
</BODY>
</HTML>
|