2011-04-28 47 views
10

我正在尋找一個非常簡短的flex和野牛工作示例,並附帶了一個使用內置規則的Makefile。我已經嘗試了幾個谷歌的結果,這些結果很混亂,不會構建,或者在C++中不可接受。良好的在線資源和簡短的示例代碼表示讚賞。Flex,Bison和C:尋找一個非常基本的介紹


附加

 # 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 

我想一個Makefile文件,看起來大約像這樣的附帶的源文件,其做一些簡單隨意。

回答

19

flex項目本身帶有一套體面的例子,包括make文件和bison文件。

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

對於一個優秀的介紹到的話題,我建議lex和yacc第二版:

http://oreilly.com/catalog/9781565920002

最後,去這裏進行快速入門:

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

編輯:

正如巴特提到,另一個來源是:http://oreilly.com/catalog/9780596155988/

而下面是我用它來啓動一個Flex項目的框架文件。它使用gnu getopts來解析命令行選項並獲取文件名。我對可移植性或易用性沒有任何要求! :)

/* 
* 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; 
} 

最後,由於不斷有人檢查了這一點,我也有一個example lexer and parser與GitHub上生成文件。

+0

優秀的資源。也許包括http://oreilly.com/catalog/9780596155988/? – 2011-04-28 19:36:43

+0

@Bart Kiers我已經添加了您的鏈接以及我的骨骼柔性文件的源代碼。 – 2011-04-28 20:25:08

+0

偉大的,不幸的是我只能投票一次:) – 2011-04-28 20:33:02

2

你可以先看維基百科bison page。 它具有用野牛編寫的可重入解析器的完整示例代碼。它使用flex作爲詞法分析器,它也有一個關於如何使用它的示例代碼。

如果您有任何更正我感謝你提前:)

更高版本:在維基百科上的代碼是在Linux上(GCC)測試和窗(視覺工作室),並應與其他編譯器也行。

+0

這是一個體面的例子,雖然它缺少一個Makefile。本質上,我正在尋找一個Hello World類型的示例。我很熟悉其他的東西,但是沒有成功地爲flex/bison寫一個簡潔的Makefile。 – colechristensen 2011-04-28 19:38:48

+0

抱歉,我沒有在維基頁面中提供這些信息,但正如您在文件的第一部分中看到的那樣,您有用於獲取所需文件的命令,以便使用c編譯器進行編譯。您只需要在flex文件和野牛文件上運行這些命令 – INS 2011-04-28 19:40:56

+0

如果您知道如何爲Flex文件的C文件編寫makefile,那麼您有以下(在這種情況下): Parser .C:Parser.y 野牛--defines = Parser.h Parser.y Lexer.c:Lexer.l 撓曲--outfile = Lexer.c --header文件= Lexer.h Lexer.l – INS 2011-04-28 19:50:10

0

Bison documentation是一個很好的計算器例子。我用它開始與野牛。 而C++示例使用了一個flex掃描器。它很容易在C中生成。