⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language.html

📁 編譯器的accent語法分析器
💻 HTML
字号:
<HTML><HEAD><TITLE>The ACCENT Grammar Language</TITLE></HEAD><BODY bgcolor="white"><TABLE cellspacing=20>   <TR>      <TD valign="top">	 <img src="logo.gif">      </TD>      <TD valign="bottom" align="left">	 <a href="index.html">The Accent Compiler Compiler</a>         <h1>The ACCENT Grammar Language</h1>      </TD>   </TR>   <TR>      <TD align="right" valign="top">	 <!-- MENU -->	 <font face="helvetica">	 <a href="index.html">Accent</a><br>	 <a href="overview.html">Overview</a><br>	 <a href="tutorial.html">Tutorial</a><br>	 Language<br>	 <a href="installation.html">Installation</a><br>	 <a href="usage.html">Usage</a><br>	 <a href="lex.html">Lex</a><br>	 <a href="algorithms.html">Algorithms</a><br>	 <a href="distribution.html">Distribution</a><br>	 </font>      </TD>      <TD valign="top">      <!--- begin main content --><h3>Conventions</h3>The <i>Accent</i> Grammar Languageis described by rules of the form<pre>   N :      M11 M12 ...   |  M21 M22 ...      ...   ;</pre>which state that a phrase for N is composed from phrases forM11 and M12 and ...or from phrases for M21 and M22 ... , etc.<p>Terminal symbols are enclosed in double quotes, e.g. <tt>"%out"</tt>.<p>In addition, the terminal symbol <tt>identifier</tt>denotes a sequence of one or more letters, digits,and underscores ("<tt>_</tt>"),starting with a letter.<p>The terminal symbol <tt>number</tt> denotes a sequence of one or more digits.<p>The terminal symbol <tt>character_constant</tt>denotes a character constant as in the <i>C</i> language.<p>The terminal symbol <tt>c_code</tt>represents arbitrary <i>C</i> code (comments in this code must be closedand curly braces much match).<h3>Grammar</h3><pre>grammar :   global_prelude_part token_declaration_part rule_part;</pre>The <i>Accent</i> Grammar Language is the set of phrases for the symbol<tt>grammar</tt>.<h3>Global Prelude</h3><pre>global_prelude_part :   global_prelude|  empty;global_prelude :   "%prelude" block;block :   "{" c_code "}";empty :;</pre>The optional <tt>global_prelude_part</tt> serves to introduceuser defined functions, global variables, and types.The text enclosed in curly braces is insertedverbatim at the beginning of the generated program file.<h3>Token Declarations</h3><pre>token_declaration_part :   "%token" token_declaration_list ";"|  empty;token_declaration_list :   token_declaration "," token_declaration_list|  token_declaration;token_declaration :   identifier;</pre>The optional <tt>token_declaration_part</tt> introduces symbolic names forterminal symbols (tokens).A name must not appear more than once in the list.<p>These names may be used as members of grammatical rules.The actual representation of the corresponding terminal symbolsmust be defined by lexical rules that are not part of the <i>Accent</i>specification.<p>As opposed to nonterminal symbols, terminal symbols are declared without parameters. Nevertheless they have an implicit output parameterof type <tt>YYSTYPE</tt>which (if used) must be defined in the corresponding lexical rule.<p>(See <a href="lex.html"><i>Using Lex with Accent</i></a>for a discussion.)<h3>Rule Part</h3><pre>rule_part :   rule_list;rule_list :   rule rule_list|  rule;rule :   left_hand_side ":" right_hand_side ";";</pre>A nonterminal is defined by a rule thatlists one or more alternatives how to construct a phrase of the nonterminal.<p>The first rule specifies the start symbol of the grammar.The language defined by the grammar is given by the phrasesof the start symbol.<h3>Left Hand Side</h3><pre>left_hand_side :   nonterminal formal_parameter_spec;nonterminal :   identifier;</pre>The <tt>left_hand_side</tt> of a rule introduces the nonterminalthat is defined by the rule.It also specifies parameters of the nonterminal, they represent the semanticattributes of the nonterminal.<p><i>Example<pre>   List&lt;list&gt;</pre>Here the nonterminal <tt>List</tt> has an attribute <tt>list</tt>.</i><p>The value of these parameters must be defined by semantic actionsin the alternatives of the body of the rule.When the nonterminal is used as a member in the body of a rule,actual parameters are attached. Using theses parameters,the attributes of the corresponding nonterminal can be accessed.<p><i>Example<pre>   List&lt;list&gt; :      Item&lt;head&gt; List&lt;tail&gt; { *list = makelist(head, tail); }   |                        { *list = emptylist();          }   ;</pre>The nonterminals of the first alternative, <tt>Item</tt> and <tt>List</tt>have parameters <tt>head</tt> and <tt>tail</tt>, respectively.These are used in the semantic action to compute the value of parameterof the left hand side, <tt>list</tt>.</i><pre>formal_parameter_spec :   empty|  "&lt;" parameter_spec_list "&gt;"|  "&lt;" "%in" parameter_spec_list "&gt;"|  "&lt;" "%out" parameter_spec_list "&gt;"|  "&lt;" "%in" parameter_spec_list "%out" parameter_spec_list "&gt;";parameter_spec_list :   parameter_spec "," parameter_spec_list|  parameter_spec;</pre>Parameters may be of mode <tt>in</tt> or mode <tt>out</tt>.If no mode is specified, all parameters are of mode <tt>out</tt>.Otherwise, parameters are of mode <tt>in</tt> if they appear in a listpreceded by <tt>%in</tt>; they are of mode <tt>out</tt>if the list is preceded by <tt>%out</tt>.<p>An <tt>in</tt> parameter (inherited attribute) passes a valuefrom the application of a nonterminal to the right hand side definingthe symbol.It is used to pass context information to a rule.<p>An <tt>out</tt> parameter (synthesized attribute) passes a valuefrom the right hand side defining a symbol to the application of the symbol.It is used to pass the semantic value of a rule to the context.<p><i>Example<pre>DeclarationPart&lt;%out symtab&gt;:   /* ... */ ;StatementPart&lt;%in symtab %out code&gt;:   /* ... */ ;Program&lt;code&gt;:   DeclarationPart&lt;symtab&gt; StatementPart&lt;symtab, code&gt;;</pre>In the rule for <tt>Program</tt>the output parameter of <tt>DeclarationPart</tt>is passed as an input parameter to<tt>StatementPart</tt></i><p><pre>parameter_spec :   parameter_type_opt parameter_name;parameter_type_opt :   parameter_type|  empty;parameter_type :   identifier;parameter_name :   identifier;</pre>A parameter specification may be written in the form<i>type</i> <i>name</i> in which case <i>type</i>is the type of the parameter <i>name</i>.If the <i>type</i> is missing, the parameter is of type <tt>YYSTYPE</tt>(which is also the type of tokens).<tt>YYSTYPE</tt> is equivalent to <tt>long</tt> if not defined by the user.<p>(See the <a href="lex.html"><i>Using Lex with Accent</i></a> how to define<tt>YYSTYPE</tt>.)<p>The start symbol of the grammar must have no parameter.<h3>Right Hand Side</h3><pre>right_hand_side :   local_prelude_option alternative_list;</pre>The right hand side of a rule specifies a list of alternatives.This list may be preceded by a prelude thatintroduces common declarations and initialisation statement in <i>C</i>.<pre>local_prelude_option :   local_prelude|  empty;local_prelude :   "%prelude" block;</pre>In the generated program the content of <tt>block</tt>(without the enclosing parentheses)precedes the code generated for the alternatives of the rule.The items declared in the prelude are visible within all alternatives.<pre>alternative_list :   alternative "|" alternative_list|  alternative;alternative :   member_list alternative_annotation_option;member_list :   member member_list|  empty;alternative_annotation_option :   alternative_annotation|  empty;alternative_annotation :   "%prio" number;member :   member_annotation_option item;member_annotation_option :   member_annotation|  empty;member_annotation :   "%short"|  "%long";item :   symbol|  literal|  grouping|  option|  repetition|  semantic_action;</pre>The alternatives appearing on the right hand side of a rulespecify how to construct a phrase for the nonterminal of the left hand side.An alternative is a sequence of members.These members may be nonterminal symbols, token symbols,or literals (terminal symbolsthat appear verbatim in the grammar).The right hand side may be written as anregular expression constructed by grouping, option, and repetition.At all places semantic actions may be inserted.<p>Ambiguities in the grammar may be resolved by annotatingalternatives and members.<p>If two alternatives of a nonterminal can produce the same stringthen both alternatives must be postfixed by an annotation ofthe form<pre>   %prio N</pre><tt>N</tt> defines the priority of the alternative.The alternative with the higher priority is selected.<p>If the same alternative can produce can produce the same stringin more than one way because members of that alternative can coversubstrings of that string of different length,the rightmost of these members must be prefixed with an annotation of the form<pre>   %short</pre>or<pre>   %long</pre>If the member is prefixed by "<tt>%short</tt>" (resp. "<tt>%long</tt>")the variant that produces the short (resp. long) substring is selected.<h3>Nonterminal and Terminal Symbols</h3><pre>symbol :   symbol_name actual_parameters_option;symbol_name :   identifier;</pre>The symbol name must be declared as a nonterminal(by specifying a rule for the identifier)or as a token (by listing the identifier in the token declaration part).<pre>actual_parameters_option :   actual_parameters|  empty;actual_parameters :   "&lt;" actual_parameter_list "&gt;";actual_parameters_list :   actual_parameter "," actual_parameter_list|  actual_parameter;actual_parameter :   identifier;</pre>For each formal parameter of the symbol there must be a corresponding actualparameter.A parameter must be an identifier.<p>In the generated <i>C</i> code,this identifier is declared as a variable of the type of thecorresponding formal parameter. The same parameter name may be usedat different places but then the type of the positions must be identical.<pre>literal :   character_constant;</pre>Besides being declared as a token, a terminal symbol can also appear verbatimas a member of rule.<h3>Structured Members</h3><pre>grouping :   "(" alternative_list ")";</pre>A construct<pre>   ( alt_1 | alt_2 | ... )</pre>matches a phrase generated by the alternatives<tt>alt_i</tt> <pre>option :   "(" alternative_list ")?";</pre>A construct<pre>   ( alt_1 | alt_2 | ... )?</pre>matches the empty phrase or a phrase generated by the alternatives<tt>alt_i</tt> <pre>repetition :   "(" alternative_list ")*";</pre>A construct<pre>   ( alt_1 | alt_2 | ... )*</pre>matches the empty phraseor any sequence of phrases generated by the alternatives<tt>alt_i</tt> <h3>Semantic Actions</h3><pre>semantic_action :   block;</pre>Semantic actions may be inserted as members of alternatives.They do not influence the parsing process.<p>Semantic actions can contain arbitrary <i>C</i> code enclosed incurly braces.This code is executed in a second phase after the parsing process.The semantic actions of selected alternativesare executed from left to right in the given order. <p>Output parameters of preceding symbols may be accessed in the semantic action.Input parameters of following symbols must be defined.<p>Parameters are accessed by specifying their names.The name of the output parameters of the left hand side must be precededby a dereferencing operator ('<tt>*</tt>').<p>In the generated programthe curly braces enclosing the action do not appear in the generated program(hence a semantic action at the beginning of an alternativemay contain declarations of variables that local to the alternative).      <!--- end main content -->      <br>      <br>      <font face="helvetica" size="1">      <a href="http://accent.compilertools.net">accent.compilertools.net</a>      </font>      </TD>   </TR></TABLE></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -