iyacc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. .TH YACC 1
  2. .SH NAME
  3. yacc \- yet another compiler-compiler (Limbo version)
  4. .SH SYNOPSIS
  5. .B yacc
  6. [
  7. .I option ...
  8. ]
  9. .I grammar
  10. .SH DESCRIPTION
  11. .I Yacc
  12. converts a context-free grammar and translation code
  13. into a set of
  14. tables for an LR(1) parser and translator.
  15. The grammar may be ambiguous;
  16. specified precedence rules are used to break ambiguities.
  17. .PP
  18. The output from
  19. .I yacc
  20. is a Limbo module
  21. .B y.tab.b
  22. containing the parse function
  23. .B yyparse
  24. which must be provided with a
  25. .B YYLEX
  26. adt providing the parser access to a lexical analyser
  27. routine
  28. .BR lex() ,
  29. an error routine
  30. .BR error() ,
  31. and any other context required.
  32. .PP
  33. The options are
  34. .TP "\w'\fL-o \fIoutput\fLXX'u"
  35. .BI -o " output
  36. Direct output to the specified file instead of
  37. .BR y.tab.b .
  38. .TP
  39. .BI -D n
  40. Create file
  41. .BR y.debug ,
  42. containing diagnostic messages.
  43. To incorporate them in the parser, give an
  44. .I n
  45. greater than zero.
  46. The amount of
  47. diagnostic output from the parser is regulated by
  48. value
  49. .IR n :
  50. .RS
  51. .TP
  52. 1
  53. Report errors.
  54. .TP
  55. 2
  56. Also report reductions.
  57. .TP
  58. 3
  59. Also report the name of each token returned by
  60. .LR yylex .
  61. .RE
  62. .TP
  63. .B -v
  64. Create file
  65. .BR y.output ,
  66. containing a description of the parsing tables and of
  67. conflicts arising from ambiguities in the grammar.
  68. .TP
  69. .B -d
  70. Create file
  71. .BR y.tab.m ,
  72. containing the module
  73. declaration for the parser, along with
  74. definitions of the constants
  75. that associate
  76. .IR yacc -assigned
  77. `token codes' with user-declared `token names'.
  78. Include it in source files other than
  79. .B y.tab.b
  80. to give access to the token codes and the parser module.
  81. .TP
  82. .BI -s " stem
  83. Change the prefix
  84. .L y
  85. of the file names
  86. .BR y.tab.b ,
  87. .BR y.tab.m ,
  88. .BR y.debug ,
  89. and
  90. .B y.output
  91. to
  92. .IR stem .
  93. .TP
  94. .B -m
  95. Normally
  96. .I yacc
  97. defines the type of the
  98. .B y.tab.b
  99. module within the text of the module according
  100. to the contents of the
  101. .B %module
  102. directive.
  103. Giving the
  104. .B -m
  105. option suppresses this behaviour, leaving the implementation
  106. free to define the module's type from an external
  107. .B .m
  108. file. The module's type name is still taken from the
  109. .B %module
  110. directive.
  111. .TP
  112. .BI -n " size
  113. Specify the initial
  114. .I size
  115. of the token stack created for the
  116. parser (default: 200).
  117. .SS Differences from C yacc
  118. The Limbo
  119. .I yacc
  120. is in many respects identical to the C
  121. .IR yacc .
  122. The differences are summarised below:
  123. .PP
  124. Comments follow the Limbo convention (a
  125. .B #
  126. symbol gives a comment until the end of the line).
  127. .PP
  128. A
  129. .B %module
  130. directive is required, which replaces the
  131. .B %union
  132. directive. It is of the form:
  133. .RS
  134. .IP
  135. .B %module
  136. .I modname
  137. .B {
  138. .br
  139. .I module types, functions and constants
  140. .br
  141. .B }
  142. .RE
  143. .B Modname
  144. will be the module's implementation type;
  145. the body of the directive, augmented with
  146. .B con
  147. definitions for the
  148. .IR yacc -assigned
  149. token codes, gives the type of the module,
  150. unless the
  151. .B -m
  152. option is given, in which case no module
  153. definition is emitted.
  154. .PP
  155. A type
  156. .B YYSTYPE
  157. must be defined, giving the type
  158. associated with
  159. .I yacc
  160. tokens. If
  161. the angle bracket construction is used after
  162. any of the
  163. .BR %token ,
  164. .BR %left ,
  165. .BR %right ,
  166. .BR %nonassoc
  167. or
  168. .B %type
  169. directives in order to associate a type with a token or production,
  170. the word inside the angle brackets
  171. refers to a member of
  172. an instance of
  173. .BR YYSTYPE ,
  174. which should be an adt.
  175. .PP
  176. An adt
  177. .B YYLEX
  178. must be defined, providing context to the parser.
  179. The definition must consist of at least the following:
  180. .EX
  181. YYLEX: adt {
  182. lval: YYSTYPE;
  183. lex: fn(l: self ref YYLEX): int;
  184. error: fn(l: self ref YYLEX, msg: string);
  185. }
  186. .EE
  187. .B Lex
  188. should invoke a lexical analyser to return the
  189. next token for
  190. .I yacc
  191. to analyse. The value of the token should
  192. be left in
  193. .BR lval .
  194. .B Error
  195. will be called when a parse error occurs.
  196. .B Msg
  197. is a string describing the error.
  198. .PP
  199. .B Yyparse
  200. takes one argument, a reference to the
  201. .B YYLEX
  202. adt that will be used to provide it with tokens.
  203. .PP
  204. The parser is fully re-entrant;
  205. .I i.e.
  206. it does not
  207. hold any parse state in any global variables
  208. within the module.
  209. .SH EXAMPLE
  210. The following is a small but complete example of the
  211. use of Limbo
  212. .I yacc
  213. to build a simple calculator.
  214. .EX
  215. %{
  216. include "sys.m";
  217. sys: Sys;
  218. include "bufio.m";
  219. bufio: Bufio;
  220. Iobuf: import bufio;
  221. include "draw.m";
  222. YYSTYPE: adt { v: real; };
  223. YYLEX: adt {
  224. lval: YYSTYPE;
  225. lex: fn(l: self ref YYLEX): int;
  226. error: fn(l: self ref YYLEX, msg: string);
  227. };
  228. %}
  229. %module Calc{
  230. init: fn(ctxt: ref Draw->Context, args: list of string);
  231. }
  232. %left '+' '-'
  233. %left '*' '/'
  234. %type <v> exp uexp term
  235. %token <v> REAL
  236. %%
  237. top :
  238. | top '\en'
  239. | top exp '\en'
  240. {
  241. sys->print("%g\en", $2);
  242. }
  243. | top error '\en'
  244. ;
  245. exp : uexp
  246. | exp '*' exp { $$ = $1 * $3; }
  247. | exp '/' exp { $$ = $1 / $3; }
  248. | exp '+' exp { $$ = $1 + $3; }
  249. | exp '-' exp { $$ = $1 - $3; }
  250. ;
  251. uexp : term
  252. | '+' uexp { $$ = $2; }
  253. | '-' uexp { $$ = -$2; }
  254. ;
  255. term : REAL
  256. | '(' exp ')'
  257. {
  258. $$ = $2;
  259. }
  260. ;
  261. %%
  262. in: ref Iobuf;
  263. stderr: ref Sys->FD;
  264. init(nil: ref Draw->Context, nil: list of string)
  265. {
  266. sys = load Sys Sys->PATH;
  267. bufio = load Bufio Bufio->PATH;
  268. in = bufio->fopen(sys->fildes(0), Bufio->OREAD);
  269. stderr = sys->fildes(2);
  270. lex := ref YYLEX;
  271. yyparse(lex);
  272. }
  273. YYLEX.error(nil: self ref YYLEX, err: string)
  274. {
  275. sys->fprint(stderr, "%s\en", err);
  276. }
  277. YYLEX.lex(lex: self ref YYLEX): int
  278. {
  279. for(;;){
  280. c := in.getc();
  281. case c{
  282. ' ' or '\et' =>
  283. ;
  284. '-' or '+' or '*' or '/' or '\en' or '(' or ')' =>
  285. return c;
  286. '0' to '9' or '.' =>
  287. s := "";
  288. i := 0;
  289. s[i++] = c;
  290. while((c = in.getc()) >= '0' && c <= '9' ||
  291. c == '.' ||
  292. c == 'e' || c == 'E')
  293. s[i++] = c;
  294. in.ungetc();
  295. lex.lval.v = real s;
  296. return REAL;
  297. * =>
  298. return -1;
  299. }
  300. }
  301. }
  302. .EE
  303. .SH FILES
  304. .TF /lib/yaccpar
  305. .TP
  306. .B y.output
  307. .TP
  308. .B y.tab.b
  309. .TP
  310. .B y.tab.m
  311. .TP
  312. .B y.debug
  313. .TP
  314. .B /lib/yaccpar
  315. parser prototype
  316. .SH SOURCE
  317. .B /appl/cmd/yacc.b
  318. .SH "SEE ALSO"
  319. S. C. Johnson and R. Sethi,
  320. ``Yacc: A parser generator'',
  321. .I
  322. Unix Research System Programmer's Manual,
  323. Tenth Edition, Volume 2
  324. .br
  325. B. W. Kernighan and Rob Pike,
  326. .I
  327. The UNIX Programming Environment,
  328. Prentice Hall, 1984
  329. .SH BUGS
  330. The parser may not have full information when it writes to
  331. .B y.debug
  332. so that the names of the tokens returned by
  333. .L yylex
  334. may be missing.