pars.g 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* This file contains the description of the 'as_table' parser.
  2. * It transforms every entry into a C-funtion, for example :
  3. *
  4. * and dst:REG, src:EADDR ==> @text1( 0x23);
  5. * mod_RM( dst->reg, src).
  6. *
  7. * ... dst:ACCU, src:DATA ==> @text1( 0x25);
  8. * @text2(
  9. * %$(src->expr)).
  10. *
  11. * Will be transformed into :
  12. *
  13. * and_instr( dst, src)
  14. * struct t_operand *dst, *src;
  15. * {
  16. * if ( REG( dst) && EADDR( src)) {
  17. * cur_pos += 1;
  18. * fprint( outfile, "text1( 0x23)");
  19. * fprint( outfile, ";");
  20. * mod_RM( dst->reg, src);
  21. * }
  22. * else if ( ACCU( dst) && DATA( src)) {
  23. * cur_pos += 1;
  24. * fprint( outfile, "text1( 0x25)");
  25. * fprint( outfile, ";");
  26. * cur_pos += 2;
  27. * fprint( outfile, "text2( ");
  28. * eval( src->expr);
  29. * fprint( outfile, ")");
  30. * fprint( outfile, ";");
  31. * }
  32. * else
  33. * error( "No match for and");
  34. * }
  35. *
  36. * At the end of the table a list is generated enumerating all the assembler
  37. * mnemonics and their corresponding function names.
  38. */
  39. {
  40. #include "decl.h"
  41. extern int lineno, yyleng;
  42. #ifdef FLEX
  43. extern char *yytext;
  44. #else
  45. extern char yytext[];
  46. #endif
  47. }
  48. %token IDENTIFIER, CALL, CONDITION, IF, ELSIF, ELSE, FI, ARROW, MORE;
  49. %start table, table;
  50. %lexical lex_analyzer ;
  51. table : { init_table();}
  52. instruction* { end_table();}
  53. ;
  54. instruction : { clean();}
  55. first_row
  56. [ { operand_clean();}
  57. extra_row
  58. ]* { pr_warning(); out( "}\n\n");}
  59. ;
  60. first_row : mnemonic { save_instr( yytext, yyleng);}
  61. [ decl_list]?
  62. ARROW { pr_header(); pr_restriction();}
  63. action_list
  64. ;
  65. extra_row : MORE
  66. [ decl_list]?
  67. ARROW { out( "else "); pr_restriction();}
  68. action_list
  69. ;
  70. mnemonic : IDENTIFIER
  71. ;
  72. decl_list : { clear_restriction();}
  73. declaration
  74. [ ',' declaration] *7
  75. ;
  76. declaration : IDENTIFIER { save_name( yytext, yyleng);}
  77. [ ':'
  78. IDENTIFIER { save_type( yytext, yyleng);}
  79. ]? { inc_ops();}
  80. ;
  81. action_list : { out( "{\n");}
  82. [ action [ ';' action]* ]? '.' { out( "}\n");}
  83. ;
  84. action : if_statement
  85. | call
  86. | subroutine
  87. ;
  88. /* A function call is just an identifier followed by an expression surrounded
  89. * by '(' and ')'. CONDITION is a token that matches this construct;
  90. */
  91. subroutine
  92. { char *s; } : IDENTIFIER { s = Salloc(yytext, yyleng+1); }
  93. CONDITION { s = Realloc(s, strlen(s)+yyleng+1);
  94. strcat(s, yytext);
  95. pr_subroutine( s);
  96. free(s);
  97. }
  98. ;
  99. call
  100. { char *s; } : '@'
  101. IDENTIFIER { s = Salloc(yytext, yyleng+1); }
  102. CONDITION { s = Realloc(s, strlen(s)+yyleng+1);
  103. strcat(s, yytext);
  104. pr_call( s);
  105. free(s);
  106. }
  107. ;
  108. if_statement : IF
  109. CONDITION { pr_question( yytext);}
  110. action_list { pr_end();}
  111. [ ELSIF { pr_els();}
  112. CONDITION { pr_question( yytext);}
  113. action_list { pr_end();}
  114. ]*
  115. [ ELSE { pr_else();}
  116. action_list { pr_end();}
  117. ]?
  118. FI
  119. ;
  120. {
  121. int nerrors;
  122. static int saved = 0, token;
  123. LLmessage( inserted_token)
  124. int inserted_token;
  125. {
  126. nerrors++;
  127. if ( inserted_token == 0) {
  128. fprint( STDERR, "Sytax error in line %d, ", lineno);
  129. print_token( LLsymb);
  130. fprint( STDERR, " will be deleted!!\n");
  131. }
  132. else if ( inserted_token < 0) {
  133. fprint( STDERR, "Garbage at end, line %d!!\n",
  134. lineno);
  135. }
  136. else {
  137. fprint( STDERR, "Sytax error in line %d, ", lineno);
  138. print_token( inserted_token);
  139. fprint( STDERR, " will be inserted!!\n");
  140. token = LLsymb;
  141. saved = 1;
  142. }
  143. }
  144. print_token( token)
  145. int token;
  146. {
  147. switch ( token) {
  148. case IDENTIFIER : fprint( STDERR, "IDENTIFIER %s", yytext);
  149. break;
  150. case CALL : fprint( STDERR, "CALL %s", yytext);
  151. break;
  152. case CONDITION: fprint( STDERR, "CONDITION %s", yytext);
  153. break;
  154. case IF : fprint( STDERR, "@if ");
  155. break;
  156. case ELSIF : fprint( STDERR, "@elsif ");
  157. break;
  158. case ELSE : fprint( STDERR, "@else ");
  159. break;
  160. case FI : fprint( STDERR, "@fi ");
  161. break;
  162. case ARROW : fprint( STDERR, "==> ");
  163. break;
  164. case MORE : fprint( STDERR, "... ");
  165. break;
  166. default : fprint( STDERR, "%c ", token);
  167. break;
  168. }
  169. }
  170. int lex_analyzer()
  171. {
  172. int tok;
  173. if ( saved) {
  174. saved = 0;
  175. return( token);
  176. }
  177. else {
  178. tok = yylex();
  179. yytext[yyleng] = '\0'; /* strings must end with '\0' */
  180. return( tok);
  181. }
  182. }
  183. main()
  184. {
  185. table();
  186. exit(nerrors);
  187. }
  188. }