Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4560450
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:55:55+00:00 2026-05-21T17:55:55+00:00

I am looking for a very short working example of flex and bison with

  • 0

I am looking for a very short working example of flex and bison with an accompanying Makefile which makes use of the builtin rules. I’ve tried several google results that were messy, wouldn’t build, or were in C++ which isn’t acceptable. Good online resources and short sample code is appreciated.


ADDITIONAL

     # 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

I would like a Makefile which looks approximately like this with attached source files which do something arbitrarily simple.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:55:55+00:00Added an answer on May 21, 2026 at 5:55 pm

    The flex project itself comes with a decent set of examples, including make files and bison files.

    https://github.com/westes/flex/releases

    For an excellent intro to the topic, I suggest lex and yacc 2nd edition:

    http://oreilly.com/catalog/9781565920002

    Finally, go here for a quick primer:

    http://ds9a.nl/lex-yacc/cvs/lex-yacc-howto.html

    Edit:

    As Bart mentioned, another source is: http://oreilly.com/catalog/9780596155988/

    And the following is the skeleton file I use to start a flex project. It uses gnu getopts for parsing command line options and getting the file name. I make no claims as to portability or ease of use! 🙂

    /*
     * This file is part of flex.
     * 
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     * 
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     * 
     * Neither the name of the University nor the names of its contributors
     * may be used to endorse or promote products derived from this software
     * without specific prior written permission.
     * 
     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
     * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
     * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE.
     */
    
        /************************************************** 
            start of definitions section
    
        ***************************************************/
    
    %{
    /* A template scanner file to build "scanner.c". */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <getopt.h>
    /*#include "parser.h" */
    
    //put your variables here
    char FileName[256];
    FILE *outfile;
    char **myOut;
    char inputName[256];
    
    
    
    
    // flags for command line options
    static int specificFile_flag = 0;
    static int output_flag = 0;
    static int help_flag = 0;
    
    %}
    
    
    %option 8bit outfile="scanner.c"
    %option nounput nomain noyywrap 
    %option warn
    
    %x header
    %x fileType
    %x final
    
    %%
        /************************************************ 
            start of rules section
    
        *************************************************/
    
    
        /* these flex patterns will eat all input */ 
    . { }
    \n { }
    
    
    %%
        /**************************************************** 
            start of code section
    
    
        *****************************************************/
    
    int main(int argc, char **argv);
    
    int main (argc,argv)
    int argc;
    char **argv;
    {
        /****************************************************
            The main method drives the program. It gets the filename from the
            command line, and opens the initial files to write to. Then it calls the lexer.
            After the lexer returns, the main method finishes out the report file,
            closes all of the open files, and prints out to the command line to let the
            user know it is finished.
        ****************************************************/
    
        int c;
    
        // the gnu getopt library is used to parse the command line for flags
        // afterwards, the final option is assumed to be the input file
    
        while (1) {
            static struct option long_options[] = {
                /* These options set a flag. */
                {"specific-file", no_argument,       &specificFile_flag, 1},
                {"help",   no_argument,     &help_flag, 1},
                /* These options don't set a flag. We distinguish them by their indices. */
    
                {"debug", no_argument,       0, 'd'},
                {"specificFile", no_argument,       0, 's'},
                {"useStdOut", no_argument,       0, 'o'},
                {0, 0, 0, 0}
            };
               /* getopt_long stores the option index here. */
            int option_index = 0;
            c = getopt_long (argc, argv, "dso",
                long_options, &option_index);
    
            /* Detect the end of the options. */
            if (c == -1)
                break;
    
            switch (c) {
                case 0:
                   /* If this option set a flag, do nothing else now. */
                   if (long_options[option_index].flag != 0)
                     break;
                   printf ("option %s", long_options[option_index].name);
                   if (optarg)
                     printf (" with arg %s", optarg);
                   printf ("\n");
                   break;
    
                case 'd':
                    break;
    
                case 's':
                   specificFile_flag = 1;
                   break;
    
                case 'o':
                   output_flag = 1;
                   break;
    
    
                case '?':
                   /* getopt_long already printed an error message. */
                   break;
    
                default:
                   abort ();
                }
        }
    
        if (help_flag == 1) {
            printf("proper syntax is: addressGrabber.exe [OPTIONS]... INFILE OUTFILE\n");
            printf("grabs address from prn files\n\n");
            printf("Option list: \n");
            printf("-s    --specific-file   changes INFILE from a prn list to a specific prn\n");
            printf("-d              turns on debug information\n");
            printf("-o                      sets output to stdout\n");
            printf("--help                  print help to screen\n");
            printf("\n");
            printf("list example: addressGrabber.exe list.csv\n");
            printf("prn example: addressGrabber.exe -s 01110500.prn\n\n");
            printf("If infile is left out, then stdin is used for input.\n");
            printf("If outfile is a filename, then that file is used.\n");
            printf("If there is no outfile, then infile-EDIT.tab is used.\n");
            printf("There cannot be an outfile without an infile.\n");
            return 0;
        }
    
        //get the filename off the command line and redirect it to input
        //if there is no filename or it is a - then use stdin
    
    
        if (optind < argc) {
            FILE *file;
    
            file = fopen(argv[optind], "rb");
            if (!file) {
                fprintf(stderr, "Flex could not open %s\n",argv[optind]);
                exit(1);
            }
            yyin = file;
            strcpy(inputName, argv[optind]);
        }
        else {
            printf("no input file set, using stdin. Press ctrl-c to quit");
            yyin = stdin;
            strcpy(inputName, "\b\b\b\b\bagainst stdin");
        }
    
        //increment current place in argument list
        optind++;
    
    
        /********************************************
            if no input name, then output set to stdout
            if no output name then copy input name and add -EDIT.csv
            if input name is '-' then output set to stdout
            otherwise use output name
    
        *********************************************/
        if (optind > argc) {
            yyout = stdout;
        }   
        else if (output_flag == 1) {
            yyout = stdout;
        }
        else if (optind < argc){
            outfile = fopen(argv[optind], "wb");
            if (!outfile) {
                    fprintf(stderr, "Flex could not open %s\n",FileName);
                    exit(1);
                }
            yyout = outfile;
        }
        else {
            strncpy(FileName, argv[optind-1], strlen(argv[optind-1])-4);
            FileName[strlen(argv[optind-1])-4] = '\0';
            strcat(FileName, "-EDIT.tab");
            outfile = fopen(FileName, "wb");
            if (!outfile) {
                    fprintf(stderr, "Flex could not open %s\n",FileName);
                    exit(1);
                }
            yyout = outfile;
        }
    
    
        yylex();
        if (output_flag == 0) {
            fclose(yyout);
        }
        printf("Flex program finished running file %s\n", inputName);
        return 0;
    }
    

    Finally, since people keep checking this out, I also have an example lexer and parser with makefile on github.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for an asymmetric cipher which has the property that very short
I'm looking for a very private source control/hosting solution. Short of hosting my own,
I'm looking for a very simple example of active record in .NET (code samples
I'm looking for a very short code to perform this in JavaScript(jQuery): date(r,hexdec(substr(uniqid(),0,8))); Please
Please take a look at this very short example fieldset, legend, .TLabel, .TComboBox, .select,
I'm looking for a way to convert a BigInteger into a very short String
Any silverlight text to speech engine available now? I am looking for very simple
I am looking for a very fast way to filter down a collection in
I'm looking for a very simple tool to monitor the bandwidth of all my
I'm looking for a very specific eclipse plugin that will tell me if a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.