pars.g 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <stdlib.h>
  41. #include <string.h>
  42. #include "decl.h"
  43. extern int lineno, yyleng;
  44. #ifdef FLEX
  45. extern char *yytext;
  46. #else
  47. extern char yytext[];
  48. #endif
  49. }
  50. %token IDENTIFIER, CALL, CONDITION, IF, ELSIF, ELSE, FI, ARROW, MORE;
  51. %start table, table;
  52. %lexical lex_analyzer ;
  53. table : { init_table();}
  54. instruction* { end_table();}
  55. ;
  56. instruction : { clean();}
  57. first_row
  58. [ { operand_clean();}
  59. extra_row
  60. ]* { pr_warning(); out( "}\n\n");}
  61. ;
  62. first_row : mnemonic { save_instr( yytext, yyleng);}
  63. [ decl_list]?
  64. ARROW { pr_header(); pr_restriction();}
  65. action_list
  66. ;
  67. extra_row : MORE
  68. [ decl_list]?
  69. ARROW { out( "else "); pr_restriction();}
  70. action_list
  71. ;
  72. mnemonic : IDENTIFIER
  73. ;
  74. decl_list : { clear_restriction();}
  75. declaration
  76. [ ',' declaration] *7
  77. ;
  78. declaration : IDENTIFIER { save_name( yytext, yyleng);}
  79. [ ':'
  80. IDENTIFIER { save_type( yytext, yyleng);}
  81. ]? { inc_ops();}
  82. ;
  83. action_list : { out( "{\n");}
  84. [ action [ ';' action]* ]? '.' { out( "}\n");}
  85. ;
  86. action : if_statement
  87. | call
  88. | subroutine
  89. ;
  90. /* A function call is just an identifier followed by an expression surrounded
  91. * by '(' and ')'. CONDITION is a token that matches this construct;
  92. */
  93. subroutine
  94. { char *s; } : IDENTIFIER { s = Salloc(yytext, yyleng+1); }
  95. CONDITION { s = Realloc(s, strlen(s)+yyleng+1);
  96. strcat(s, yytext);
  97. pr_subroutine( s);
  98. free(s);
  99. }
  100. ;
  101. call
  102. { char *s; } : '@'
  103. IDENTIFIER { s = Salloc(yytext, yyleng+1); }
  104. CONDITION { s = Realloc(s, strlen(s)+yyleng+1);
  105. strcat(s, yytext);
  106. pr_call( s);
  107. free(s);
  108. }
  109. ;
  110. if_statement : IF
  111. CONDITION { pr_question( yytext);}
  112. action_list { pr_end();}
  113. [ ELSIF { pr_els();}
  114. CONDITION { pr_question( yytext);}
  115. action_list { pr_end();}
  116. ]*
  117. [ ELSE { pr_else();}
  118. action_list { pr_end();}
  119. ]?
  120. FI
  121. ;
  122. {
  123. int nerrors;
  124. static int saved = 0, token;
  125. LLmessage( inserted_token)
  126. int inserted_token;
  127. {
  128. nerrors++;
  129. if ( inserted_token == 0) {
  130. fprint( STDERR, "Sytax error in line %d, ", lineno);
  131. print_token( LLsymb);
  132. fprint( STDERR, " will be deleted!!\n");
  133. }
  134. else if ( inserted_token < 0) {
  135. fprint( STDERR, "Garbage at end, line %d!!\n",
  136. lineno);
  137. }
  138. else {
  139. fprint( STDERR, "Sytax error in line %d, ", lineno);
  140. print_token( inserted_token);
  141. fprint( STDERR, " will be inserted!!\n");
  142. token = LLsymb;
  143. saved = 1;
  144. }
  145. }
  146. print_token( token)
  147. int token;
  148. {
  149. switch ( token) {
  150. case IDENTIFIER : fprint( STDERR, "IDENTIFIER %s", yytext);
  151. break;
  152. case CALL : fprint( STDERR, "CALL %s", yytext);
  153. break;
  154. case CONDITION: fprint( STDERR, "CONDITION %s", yytext);
  155. break;
  156. case IF : fprint( STDERR, "@if ");
  157. break;
  158. case ELSIF : fprint( STDERR, "@elsif ");
  159. break;
  160. case ELSE : fprint( STDERR, "@else ");
  161. break;
  162. case FI : fprint( STDERR, "@fi ");
  163. break;
  164. case ARROW : fprint( STDERR, "==> ");
  165. break;
  166. case MORE : fprint( STDERR, "... ");
  167. break;
  168. default : fprint( STDERR, "%c ", token);
  169. break;
  170. }
  171. }
  172. int lex_analyzer()
  173. {
  174. int tok;
  175. if ( saved) {
  176. saved = 0;
  177. return( token);
  178. }
  179. else {
  180. tok = yylex();
  181. yytext[yyleng] = '\0'; /* strings must end with '\0' */
  182. return( tok);
  183. }
  184. }
  185. main()
  186. {
  187. table();
  188. exit(nerrors);
  189. }
  190. }