我正在寻找一个非常简短的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,附带的源文件可以做任意简单的事情。
flex项目本身带有一组不错的示例,包括make文件和bison文件。
有关该主题的优秀介绍,我建议lex和yacc第2版:
最后,到这里快速入门:
编辑:
正如Bart提到的,另一个来源是: 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 #include #include #include /*#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 OUTFILEn"); printf("grabs address from prn filesnn"); printf("Option list: n"); printf("-s --specific-file changes INFILE from a prn list to a specific prnn"); printf("-d turns on debug informationn"); printf("-o sets output to stdoutn"); printf("--help print help to screenn"); printf("n"); printf("list example: addressGrabber.exe list.csvn"); printf("prn example: addressGrabber.exe -s 01110500.prnnn"); 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 %sn",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, "bbbbbagainst 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 %sn",FileName); exit(1); } yyout = outfile; } else { strncpy(FileName, argv[optind-1], strlen(argv[optind-1])-4); FileName[strlen(argv[optind-1])-4] = ' '; strcat(FileName, "-EDIT.tab"); outfile = fopen(FileName, "wb"); if (!outfile) { fprintf(stderr, "Flex could not open %sn",FileName); exit(1); } yyout = outfile; } yylex(); if (output_flag == 0) { fclose(yyout); } printf("Flex program finished running file %sn", inputName); return 0; }
最后,由于人们不断检查这一点,我还有一个示例lexer和解析器与github上的makefile。
您可以从查看维基百科野牛页面开始 。 它有一个用bison写的可重入解析器的完整示例代码。 它使用flex作为词法分析器,它还有一个如何使用它的示例代码。
如果你有任何更正我提前谢谢你:)
后来:维基百科上的代码在linux(gcc)和windows(visual studio)上进行了测试,也应该与其他编译器一起使用。
GNU手册怎么样?
Bison 文档非常适用于计算器的一个很好的例子。 我用它来开始野牛。 C ++示例使用flex扫描程序。 在C中制作它很容易。
编译器:原理,技术和工具 ,Alfred V. Aho,Ravi Sethi,Jeffrey D. Ullman
需要了解更多c/c++开发分享Flex,Bison和C:寻找一个非常基本的介绍,也可以关注C/ C++技术分享栏目---计算机技术网(www.ctvol.com)!
以上就是c/c++开发分享Flex,Bison和C:寻找一个非常基本的介绍相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/980354.html