Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flex、Bison和C:寻找一个非常基本的介绍_C_Parsing_Bison - Fatal编程技术网

Flex、Bison和C:寻找一个非常基本的介绍

Flex、Bison和C:寻找一个非常基本的介绍,c,parsing,bison,C,Parsing,Bison,我正在寻找一个非常简短的flex和bison的工作示例,并附带一个使用内置规则的Makefile。我尝试了几个谷歌的结果,这些结果很混乱,不会建立,或者是C++,这是不可接受的。良好的在线资源和简短的示例代码是赞赏 附加的 # Makefile example -- scanner and parser. # Creates "myprogram" from "scan.l", "parse.y", and "myprogram.c" # LEX

我正在寻找一个非常简短的flex和bison的工作示例,并附带一个使用内置规则的Makefile。我尝试了几个谷歌的结果,这些结果很混乱,不会建立,或者是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,附带的源文件可以做一些任意简单的事情。

怎么样?

你可以先看看维基百科。 它有一个用bison编写的可重入解析器的完整示例代码。它使用flex作为lexer,并且它还有一个关于如何使用它的示例代码

如果您有任何更正,我将提前向您表示感谢:)

稍后:wikipedia上的代码在linux(gcc)和windows(visual studio)上进行了测试,应该可以与其他编译器一起使用。

Bison是一个非常好的计算器示例。我是从野牛开始的。
C++示例使用Flex扫描仪。在C语言中很容易实现。

flex项目本身提供了一组不错的示例,包括make文件和bison文件

对于这一主题的精彩介绍,我建议lex和yacc第二版:

最后,到这里快速入门:

编辑:

如巴特所述,另一个来源是:

下面是我用来启动flex项目的框架文件。它使用gnugetopts解析命令行选项并获取文件名。我不主张便携性或易用性!:)

/*
*此文件是flex的一部分。
* 
*以源代码和二进制形式重新分发和使用,带或不带
*如果满足以下条件,则允许进行修改
*满足以下条件:
* 
* 1. 源代码的重新分发必须保留上述版权
*请注意,此条件列表和以下免责声明。
* 2. 以二进制形式重新分发必须复制上述版权
*请注意,此条件列表和中的以下免责声明
*随分发提供的文件和/或其他材料。
* 
*既不是大学的名称,也不是贡献者的名称
*可用于认可或推广源自本软件的产品
*未经事先书面许可。
* 
*本软件按“原样”提供,无任何明示或暗示
*默示保证,包括但不限于
*特定产品的适销性和适用性保证
*目的。
*/
/************************************************** 
定义部分的开头
***************************************************/
%{
/*用于构建“scanner.c”的模板扫描程序文件*/
#包括
#包括
#包括
#包括
/*#包括“parser.h”*/
//把变量放在这里
字符文件名[256];
文件*输出文件;
字符**myOut;
字符输入名[256];
//命令行选项的标志
静态int specificFile_标志=0;
静态int输出_标志=0;
静态int help_标志=0;
%}
%选项8bit outfile=“scanner.c”
%选择权在noyywrap中
%选项警告
%x头
%x文件类型
%x决赛
%%
/************************************************ 
规则部分的开始
*************************************************/
/*这些flex模式将吃掉所有输入*/
. { }
\n{}
%%
/**************************************************** 
代码段开头
*****************************************************/
int main(int argc,字符**argv);
int main(argc、argv)
int-argc;
字符**argv;
{
/****************************************************
main方法驱动程序。它从
命令行,并打开要写入的初始文件。然后它调用lexer。
lexer返回后,main方法完成报告文件,
关闭所有打开的文件,并打印到命令行,以便
用户知道它已经完成了。
****************************************************/
INTC;
//gnugetopt库用于解析命令行中的标志
//之后,假设最后一个选项是输入文件
而(1){
静态结构选项长_选项[]={
/*这些选项设置了一个标志*/
{“specific file”,无参数,&specific file_标志,1},
{“help”,无参数,&help\u标志,1},
/*这些选项没有设置标志。我们通过它们的索引来区分它们*/
{“debug”,无_参数,0,'d'},
{“specificFile”,无_参数,0,'s'},
{“useStdOut”,无参数,0,'o'},
{0, 0, 0, 0}
};
/*getopt_long将选项索引存储在此处*/
int option_index=0;
c=getopt_long(argc,argv,“dso”,
多头期权和期权指数);
/*检测选项的结尾*/
如果(c==-1)
打破
开关(c){
案例0:
/*如果此选项设置了标志,则现在不执行其他操作*/
if(长选项[选项索引].flag!=0)
打破
printf(“选项%s”,长选项[选项索引].name);
如果(optarg)
printf(“带有参数%s”,optarg);
printf(“\n”);
打破
案例“d”:
打破
案例s:
specificFile_标志=1;
打破
案例“o”:
输出_标志=1;
打破
案例“?”:
/*getopt_long已打印错误消息*/
打破
违约:
中止();
}
}
如果(帮助标志==1){
printf(“正确的语法是:addressGrabber.exe[OPTIONS]…infle OUTFILE\n”);
printf(“从prn文件中获取地址\n\n”);
/*
 * 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;
}