parsing - yylval and union -
What is the purpose of the union in yacc file? Is this directly related to Yilwell in the Flex file? If you do not use Yellwell, you do not need to use the union?
Its purpose is to consolidate various types of objects in nodes emitted by union
flex. Are allowed to
For example, to better understand you:
% union {int intValue; Float flat value; Four * stringValue; }
in .y
if you want to give basic support for int
, float
and string
type. What can you do with it?
Two things:
First of all, you can set the correct value automatically when generating a token. Think about the file .l
of the previous example, you can have it:
[a-zA-Z] [a-zA-Z0-9 ] * {Yellwell .stringValue = strdup (yytext); Return identifier; } [0- 9] + {yylval.intValue = etymology}; Return INTEGER; } [0- 9] *. [0- 9] + "F"? {Yylval.floatValue = new yytext; Return floyet; }
Additionally you can use the value directly in your flex grammar :
nexp: nexp '+' Nexp {$ & lt; FloatValue & gt; $ = $ & Lt; Float value & gt; 1 + $ & lt; Float value & gt; 3}
In the end, if you plan to use OOP syntax tree, then you should call the union
% union {class ASTNode * node; }
in which ASTNode
is the predecessor of any kind of syntax node.
Comments
Post a Comment