Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple makefile for lex yacc and C

I am trying to compile my program which has a lex file and a yacc file and a couple of C files.I came across this example in the Flex manual.

I have a couple of questions regarding this makefile.It doesn't specify a compiler such as gcc how does the makefile know how to create the targets such as scan.o and parse.o and myprogram.o?

     # Makefile example -- scanner and parser.
     # Creates "myprogram" from "scan.l", "parse.y", and "myprogram.c"
     #
     LEX     = flex
     YACC    = bison -y
     YFLAGS  = -d
     objects = scan.o parse.o myprogram.o

     myprogram: $(objects)
     scan.o: scan.l parse.c
     parse.o: parse.y
     myprogram.o: myprogram.c
like image 662
liv2hak Avatar asked Sep 01 '15 22:09

liv2hak


People also ask

Can yacc parse C?

Yacc takes a concise description of a grammar and produces a C routine that can parse that grammar, a parser. The yacc parser automatically detects whenever a sequence of input tokens matches one of the rules in the grammar and also detects a syntax error whenever its input doesn't match any of the rules.

How does yacc and lex work together?

lex and yacc are a pair of programs that help write other programs. Input to lex and yacc describes how you want your final program to work. The output is source code in the C programming language; you can compile this source code to get a program that works the way that you originally described.

What is $$ in yacc?

those $$ , $1 , $3 are the semantic values for for the symbols and tokens used in the rule in the order that they appear. The semantic value is that one that you get in yylval when the scanner gets a new token.


1 Answers

That's a Makefile feature: some implicit rules exist and are documented in https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html#Catalogue-of-Rules

In you case, the relevant part is

Compiling C programs

n.o is made automatically from n.c with a recipe of the form ‘$(CC) $(CPPFLAGS) $(CFLAGS) -c’.

[...]

Yacc for C programs

n.c is made automatically from n.y by running Yacc with the recipe ‘$(YACC) $(YFLAGS)’.

Lex for C programs

n.c is made automatically from n.l by running Lex. The actual recipe is ‘$(LEX) $(LFLAGS)’.

According to https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_12.html some of this rules are not portable...

like image 53
serge-sans-paille Avatar answered Oct 12 '22 21:10

serge-sans-paille