MAC files grammar


Syntactic level

The following grammar is presented in the YACC format. The terminal symbol are written as capital letters, while the not terminal symbol are written as small letters.

   1  type : BYTE
   2       | WORD
   3       | DWORD
   4  name : NAME
   5  number : NUMBER
   6         | C_H_NUM
   7         | C_O_NUM
   8         | LIT_CH
   9  cost_str : COST_STRING
  10  expression : name
  11              | name '[' expression']'
  12              | ch_funz
  13              | number
  19              | '(' expression ')'
  20              | expression '+' expression
  21              | expression LSHIFT expression
  22              | expression RSHIFT expression
  23              | expression NOT_EQ expression
  24              | expression MIU expression
  25              | expression MAU expression
  26              | expression '<' expression
  27              | expression '>' expression
  28              | expression EQ expression
  29              | expression AND expression
  30              | expression OR expression
  31              | expression '-' expression
  32              | expression '*' expression
  33              | expression '/' expression
  34              | expression '%' expression
  35              | '-' expression
  36              | NOT expression
  37              | assignment
  38              | SIZEOF '(' name ')'
  39              | OFFSETOF '(' name ')'
  40              | COMMAND '(' cost_str ')'
  41              | LEN '(' name ')'
  42              | STRLEN '(' name ')'
  43  assignment : name '=' expression
  44               | name '[' expression ']' '=' expression
  45  ch_funz : name '(' l_espress ')'
  46          | name '(' ')'
  47  l_espress : expression
  48            | l_espress ',' expression
  49  programm : llinee
  50            |
  51  llinee : declarations  52         | c_function
  53         | llinee declarations  54         | llinee c_function
  55  declarations : prototype
  56  prototype : type name '(' ')' ';'
  57            | type name '(' l_variables ')' ';'
  58            | ARRAY name ';'
  59  l_variables : type name
  60              | name
  61              | l_variables ',' type name
  62              | l_variables ',' name
  63  def_var : def_v ';'
  64  def_v : type name
  65        | def_v ',' name
  66        | type error
  67  linstructions : linstructions instruction
  68              | instruction
  69              | error
  70  instruction : istruz ';'
  71             | costrutto
  72             | def_var
  73             | '{' linstructions '}'
  74             | '{' '}'
  75  costrutto : c_do
  76            | c_while
  77            | c_if
  78            | c_for
  79  do : DO
  80  while : WHILE
  81  for : FOR
  82  if : IF
  83  else : ELSE
  84  $$1 :
  85  c_do : do instruction WHILE $$1 '(' expression ')' ';'
  86       | do instruction WHILE error
  87  $$2 :
  88  c_while : while '(' expression ')' $$2 instruction
  89          | while error
  90  t_if :
  91  c_if : if '(' expression ')' t_if instruction
  92  $$3 :
  93  c_if : if '(' expression ')' t_if instruction else $$3 instruction
  94       | if error
  95  $$4 :
  96  $$5 :
  97  $$6 :
  98  c_for : for '(' expression ';' $$4 expression ';' $$5 expression ')' $$6 instruction
  99        | for error
 100  $$7 :
 101  c_function : type name '(' ')' $$7 '{' linstructions '}'
 102  $$8 :
 103  c_function : type name '(' l_variables ')' $$8 '{' linstructions '}'
 104             | type name '(' error
 105  istruz : CONTINUE
 106         | BREAK
 107         | expression
 108         | TITLE '(' cost_str ')'
 109         | TITLE '(' cost_str ',' l_espress ')'
 110         | PRINT '(' cost_str ',' l_espress ')'
 111         | PRINT '(' cost_str ')'
 112         | APRINT '(' cost_str ',' name ')'
 113         | CLEAR '(' name ')'
 114         | RETURN expression
 115         | prototype

'file' is the beginning symbol.

Here it is the table which shows the priority order of the operators in the expressions:

Low priority
Operators The operator is associated to the expression on the:
'=' right
OR left
AND left
EQ,NOT_EQ,'<','>',MIU,MAU left
LSHIFT,RSHIFT left
'+','-' left
'*','/','%' left
NOT,MENO_UNARIO right
CH_FUNZ left
High priority

Il livello lessicale

Here it is a summary of the scanner which recognize the file tokens. This summary is presented in the LEX format.

SPACE ([\t\r\n ])
SPACES ({SPACE}*)
LETTER ([a-zA-Z])
DIGIT ([0-9])
F_DIGIT ([1-9])
DECIMALE ({DIGIT}*{F_DIGIT}|0)
FINO_FINE ([^\n]*)
R_REM (\/\/)
HEX ((("0x")|("0X"))([a-fA-F]|{DIGIT})+)
OCT (("0")([0-7])+)
INTERO ({F_DIGIT}{DIGIT}*)
VAR_NAME ((("_")|{LETTER})({LETTER}|{DIGIT}|("_"))*)
COST_STRINGA (\"[^\"\n]*\")
COST_INT_CORTO ({INTERO}|[0])
QUOTE	\'([^\\']|\\[^'\\]+|\\\\)\'
OP	([-+=*/<>{}();,:%])|("\[")|("\]")
VAR ({VAR_NAME})
COMMENT ("/*"([^*]*|"*"+[^*/])"*"+\/)
COMMENT_L ("//".*)
%%
\xd	/* discard LF*/
{COMMENT}	{ /* discard comments */ }
{COMMENT_L}	{ /* discard comments */ }
do	{return DO; }
if	{return IF; }
else	{return ELSE; }
while	{return WHILE; }
for	{return FOR; }
return	{return RETURN; }
break	{return BREAK; }
continue	{return CONTINUE; }
array	{return ARRAY; }
byte	{return BYTE; }
word	{return WORD; }
dword	{return DWORD; }
sizeof	{return SIZEOF; }
offsetof	{return OFFSETOF; }
printf	{return PRINT; }
aprintf	{return APRINT; }
clear	{return CLEAR; }
arrlen	{return LEN; }
strlen	{return STRLEN; }
title	{return TITLE; }
command {return COMMAND;}
{COST_STRINGA} 	{return COST_STRINGA; }
{COST_INT_CORTO}	{return NUMERO; }
{HEX}	{return C_H_NUM; }
{OCT}	{return C_O_NUM; }
{QUOTE}	{return LIT_CH; }
AND {return AND;}
"&" {return AND;}
"&&" {return AND;}
OR {return OR;}
"|" {return OR;}
"||" {return OR;}
NOT {return NOT;}
"!" {return NOT;}
"~" {return NOT;}
{OP}	{return *yytext; }
"=="	{return EQ; }
"!="	{return NOT_EQ; }
">="	{return MAU; }
"<="	{return MIU; }
"<<"	{return LSHIFT; }
">>"	{return RSHIFT; }
{VAR}	{return NAME;}
{SPACE}+ {}
. {printf("Scanner error at line %d\n",yylineno);}
%%

'AND' can be written : 'AND', '&&', '&'

'OR' can be written : 'OR', '||', '|'

'NOT' can be written : 'NOT', '!', '~'

'=' can be written : '==', '='