Appendix I. MGL Compiler Code

Chapter 4, A Menu Generation Language , presented the lex and yacc grammars for the MGL. Here we present the entire code of the MGL, including runtime support code not shown in the chapter. Many improvements to the runtime code are possible, such as:

  • Screen clearing after improper input.

  • Better handling of the system() call, particularly saving and restoring terminal modes and screen contents.

  • Automatic generation of a main routine. Currently, it must be defined outside the calling program. Furthermore, it must call the routine menu_cleanup before exiting.

  • More flexible command handling, e.g., allowing unique prefixes of commands rather than requiring the whole command.

  • Taking keyboard input a character at a time in cbreak mode rather than the current line at a time.

  • More flexible nesting of menus.

See the Preface for information on obtaining an online copy of this code.

MGL Yacc Source

This is file mglyac.c.

 %{ #include <stdio.h> #include <string.h> #include <stdlib.h> int screen_done = 1; /* 1 if done, 0 otherwise */ char *act_str; /* extra argument for an action */ char *cmd_str; /* extra argument for command */ char *item_str; /* extra argument for * item description */ %} %union { char *string; /* string buffer */ int cmd; /* command value */ } %token <string> QSTRING ID COMMENT %token <cmd> SCREEN TITLE ITEM COMMAND ACTION EXECUTE EMPTY %token <cmd> MENU QUIT IGNORE ATTRIBUTE VISIBLE INVISIBLE END %type <cmd> action line attribute command %type <string> ...

Get lex & yacc, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.