2. YACC: Yet Another Compiler-Compiler
• Yacc generates C code for syntax analyzer, of parser.
• Yacc uses grammar rules that allow it to analyze tokens from LEX
and create a syntax tree.
• Yacc provides a general tool for describing the input to a
computer program.
• The Yacc user specifies the structures of his input, together with
code to be invoked as each such structure is recognized.
• Yacc is written in portable C.
• The class of specifications accepted is a very general one: LALR
grammars with disambiguating rules.
• In addition to compilers for C, APL, Pascal, RATFOR, etc., Yacc
has also been used for less conventional languages, including a
phototypesetter language, several desk calculator languages, a
document retrieval system, and a Fortran debugging system.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
2
3. Yacc contd…….
• Yacc is a computer program for the Unix operating system.
• It is a Look Ahead Left-to-Right (LALR) parser generator,
generating a parser.
• The part of a compiler that tries to make syntactic sense of
the source code, specifically a LALR parser, based on an analytic
grammar written in a notation similar to Backus–Naur Form
(BNF).
• Yacc was originally developed in the early 1970s by Stephen C.
Johnson at AT&T Corporation and written in the B programming
language, but soon rewritten in C.
• Yacc has also been rewritten for other languages, including
OCaml, Ratfor, ML, Ada, Pascal, Java, Python, Ruby, Go and Com
mon Lisp.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
3
4. 4
yacc
Generate a new parser code from grammar
YACC source (*.y)
y.tab.h
y.tab.c
C compiler/linker
Compile a new parser
y.tab.c a.out
a.out
Parse source code
Token stream
Abstract
Syntax
Tree
y.output
How Does YACC Work?
6. LALR PARSER GENERATOR-yacc
• The tool yacc can be used to generate automatically
an LALR parser.
• Input of yacc is divided into three sections:
………..definitions………..
%%
…..rules…..
%%
…….subroutines………..
The def section consists of token declarations & C code
bracketed by % { and % }.
The grammar is placed in the rules section, &
User subroutines are added in subroutines section.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
6
7. Example- small calculator
• The def section for the yacc input file:
%token INTEGER
This def declares an INTEGER token. When we
run yacc, it generates a parser file y.tab.c & also
creates an include file y.tab.h:
#ifndef YYSTYPE
#define YYSTYPE int
#endif
#define INTEGER 258
extern YYSTYPE yylval;
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
7
8. Contd……
• Lex includes this file & utilizes the definitions for token values.
• To obtain tokens, yacc calls yylex.
• Function yylex has a return type of int & returns the token value.
• Attributes associated with the token are returned by lex in variable
yylval.
File Content
calc.lex Specifies the lex command specification file that defines the
lexical analysis rules.
calc.yacc Specifies the yacc command grammar file that defines the
parsing rules, and calls the yylex subroutine created by
the lex command to provide input.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
8
9. Free APL Compilers and Interpreters
• ELI
ELI is an array programming language that has most of the
features of APL with additional ones not present in the language.
• NGN APL
This is an APL written in JavaScript. (Actually, it is written in
Coffee Script, which compiles to JavaScript.)
• NARS2000
NARS2000 (where NARS stands for Nested Arrays Research
System) is an experimental APL interpreter used as a test bed for
new ideas in the language.
• J
J is a programming language in the APL language family. Unlike
APL, it uses ASCII characters.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
9
10. Contd…….
• A+
According to its website, "A+ is a descendent of the language 'A'". Unlike
APL, A+ is "geared" to business, supporting large capacity and high
performance, with the "+" referring to the "electric graphical user
interface". It is released under the GNU General Public License.
Supported platforms include Linux, FreeBSD, AIX, IRIX, Mac OS X,
NetBSD, Solaris, and Tru64.
• OpenAPL
This package, for Linux, FreeBSD, OpenBSD, Mac OS X, and POSIX/Unix-
like systems, includes the APL font as well as the interpreter for APL11.
We will have to compile it from the sources provided.
• APL Special Edition
APLSE is a freeware version of APL*Plus PC, a compiler for the APL
programming language, a highly symbolic language (ie, it uses pictorial
symbols rather than the traditional English words) and compact
language.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
10