Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 


Quelle  language.html   Sprache: HTML

 
 products/sources/formale Sprachen/C/Accent/doc/language.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 Language
is described by rules of the form
<pre>
   N :
      M11 M12 ...
   |  M21 M22 ...
      ...
   ;
</pre>
which state that a phrase for N is composed from phrases for
M11 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 ("_"),
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 closed
and 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 introduce
user defined functions, global variables, and types.
The text enclosed in curly braces is inserted
verbatim 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 for
terminal 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 symbols
must 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 parameter
of 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 that
lists 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 phrases
of 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 nonterminal
that is defined by the rule.
It also specifies parameters of the nonterminal, they represent the semantic
attributes of the nonterminal.
<p>
<i>
Example
<pre>
   List<list>
</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 actions
in 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<list> :
      Item<head> List<tail> { *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 parameter
of the left hand side, <tt>list</tt>.
</i>

<pre>
formal_parameter_spec :
   empty
|  "<" parameter_spec_list ">"
|  "<" "%in" parameter_spec_list ">"
|  "<" "%out" parameter_spec_list ">"
|  "<" "%in" parameter_spec_list "%out" parameter_spec_list ">"
;

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 list
preceded 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 value
from the application of a nonterminal to the right hand side defining
the symbol.
It is used to pass context information to a rule.
<p>
An <tt>out</tt> parameter (synthesized attribute) passes a value
from 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<%out symtab>:
   /* ... */ ;
StatementPart<%in symtab %out code>:
   /* ... */ ;
Program<code>:
   DeclarationPart<symtab> StatementPart<symtab, code>;
</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 that
introduces 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 rule
specify 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 symbols
that appear verbatim in the grammar).
The right hand side may be written as an
regular expression constructed by grouping, option, and repetition.
At all places semantic actions may be inserted.
<p>
Ambiguities in the grammar may be resolved by annotating
alternatives and members.
<p>
If two alternatives of a nonterminal can produce the same string
then both alternatives must be postfixed by an annotation of
the 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 string
in more than one way because members of that alternative can cover
substrings 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 "%short" (resp. "%long")
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 :
   "<" actual_parameter_list ">"
;

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 actual
parameter.
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 the
corresponding formal parameter. The same parameter name may be used
at 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 verbatim
as 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 phrase
or 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 in
curly braces.
This code is executed in a second phase after the parsing process.
The semantic actions of selected alternatives
are 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 preceded
by a dereferencing operator ('*').
<p>
In the generated program
the curly braces enclosing the action do not appear in the generated program
(hence a semantic action at the beginning of an alternative
may 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>

100%


¤ Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.0.14Bemerkung:  (vorverarbeitet)  ¤

*Bot Zugriff






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung ist noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Produkte
     Quellcodebibliothek

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....

Besucherstatistik

Besucherstatistik

Monitoring

Montastic status badge