『壹』 linux 下怎樣安裝使用 Yacc 和 Lex
yacc/lex在linux下的使用指南
鏈接:http://blog.csdn.net/ruglcc/article/details/7817619
Lex 和 Yacc 是 Unix 和Linux 下詞法和語法的分析,解析工具,有了這兩個工具,你可以自己製作想要的編譯器,也可以重新製作已有程序語言的解析器。需要注意的是linux下的這兩個工具生成的程序源碼只能是C和C++語言,當然現在早已有類似可以生成java源碼的語法分析器,如較常用的JavaCC(Java Compiler Compiler),相關內容可以去網上搜索。Lex和Yacc已被移植到windows下,現在常用的工具有Parser Generator。本文只介紹Linux 下Lex和Yacc的使用方法。
Lex介紹
Lex 通過對.lex或.l文件定義的格式生成一個C語言源碼文件,通過編譯這個源碼,就生成了.lex文件或.l文件定義的編譯器。.lex或.l文件的格式分三段:
1.全局變數聲明部分
2.詞法規則部分
3.函數定義部分
以下是一個簡單的例子:lex_example.l文件
%{ //全局聲明部分
/*林木100 linux
www.linmu100.com
*/
#include
extern char *yytext;
extern FILE *yyin;
int sem_count = 0;
%}
//規則定義部分,
%%
[a-zA-Z][a-zA-Z0-9]* {printf("WORD[%s] ", yytext);}
[a-zA-Z0-9\/.-]+ printf("FILENAME ");
\" printf("QUOTE ");
\{ printf("OBRACE ");
\} printf("EBRACE ");
; {sem_count++; printf("SEMICOLON ");}
\n printf("\n");
[ \t]+ /* ignore whitespace */;
%%
//以下為函數定義部分
int main(int avgs, char *avgr[])
{
yyin = fopen(avgr[1], "r");
if (!yyin)
{
return 0;
}
yylex();
printf("sem_count : %d\n", sem_count);
fclose(yyin);
return 1;
}
『貳』 lex生成可執行程序時候 gcc -o exec lex.yy.c -ll 出錯 錯誤提示為: /usr/lib64/gcc/x86_64-suse-linu
你可以試試:gcc -o exec lex.yy.c -lfl
『叄』 linux下有沒有什麼好的代碼統計工具
1 sloccount源代碼行數統計工具
--------------------------------------------------------------------------------
sloccount=Count Source Lines Of Code
官網 : http://www.dwheeler.com/sloccount/
1.1 Ubuntu安裝
--------------------------------------------------------------------------------
sudo apt-get install sloccount
1.2 使用
--------------------------------------------------------------------------------
sloccount [--version] [--cached] [--append] [ --datadir directory ]
[--follow] [--plicates] [--crossps] [--autogen] [--multiproject]
[--filecount] [--wide] [--details] [ --effort F E ] [ --schele F E ] [
--personcost cost ] [ --overhead overhead ] [ --addlang language ] [
--addlangall ] [--] directories
--cached
跳過計算過程,直接使用上次結果
參數
描述
–multiproject
如果該文件夾包括一系列的子文件夾,而它們中的每一個都是相對獨立開發的不同的項目,那麼使用」–multiproject」選項,評估將會正確的考慮到這一點
–filecount
顯示文件數目而非代碼行數
–details
顯示每個源文件的詳細信息
–plicates
算上所有重復的(默認情況下如果文件有相同的內容,則只算一個)
–crossps
如果頂目錄包含幾個不同的項目,並且你想把不同的項目下重復的文件在每個項目中都算上一次,則使用該選項
1.3 轉換成html文件
--------------------------------------------------------------------------------
有一個sloc2html.py可以把生成的結果轉換為帶圖形統計結果的html文件. 缺點是對中文支持不好
例如:
sloccount --wide --multiproject SourceDirectory > result.txt
sloc2html.py result.txt > result.html
再打開result.html即可看到結果形如:
下載地址 http://www.dwheeler.com/sloccount/sloc2html.py.txt
輸出樣例 http://www.dwheeler.com/sloccount/sloc2html-example.html
wget http://www.dwheeler.com/sloccount/sloc2html.py.txt -O sloc2html.py
sloc2html.py文件源代碼如下
#!/usr/bin/env python
# Written by Rasmus Toftdahl Olesen <[email protected]>
# Modified slightly by David A. Wheeler
# Released under the GNU General Public License v. 2 or higher
from string import *
import sys
NAME = "sloc2html"
VERSION = "0.0.2"
if len(sys.argv) != 2:
print "Usage:"
print "\t" + sys.argv[0] + " <sloc output file>"
print "\nThe output of sloccount should be with --wide and --multiproject formatting"
sys.exit()
colors = { "python" : "blue",
"ansic" : "yellow",
"perl" : "purple",
"cpp" : "green",
"sh" : "red",
"yacc" : "brown",
"lex" : "silver"
# Feel free to make more specific colors.
"ruby" : "maroon",
"cs" : "gray",
"java" : "navy",
"ada" : "olive",
"lisp" : "fuchsia",
"objc" : "purple",
"fortran" : "purple",
"cobol" : "purple",
"pascal" : "purple",
"asm" : "purple",
"csh" : "purple",
"tcl" : "purple",
"exp" : "purple",
"awk" : "purple",
"sed" : "purple",
"makefile" : "purple",
"sql" : "purple",
"php" : "purple",
"mola3" : "purple",
"ml" : "purple",
"haskell" : "purple"
}
print "<html>"
print "<head>"
print "<title>Counted Source Lines of Code (SLOC)</title>"
print "</head>"
print "<body>"
print "<h1>Counted Source Lines of Code</h1>"
file = open ( sys.argv[1], "r" )
print "<h2>Projects</h2>"
line = ""
while line != "SLOC\tDirectory\tSLOC-by-Language (Sorted)\n":
line = file.readline()
print "<table>"
print "<tr><th>Lines</th><th>Project</th><th>Language distribution</th></tr>"
line = file.readline()
while line != "\n":
num, project, langs = split ( line )
print "<tr><td>" + num + "</td><td>" + project + "</td><td>"
print "<table width=\"500\"><tr>"
for lang in split ( langs, "," ):
l, n = split ( lang, "=" )
print "<td bgcolor=\"" + colors[l] + "\" width=\"" + str( float(n) / float(num) * 500 ) + "\">" + l + "=" + n + " (" + str(int(float(n) / float(num) * 100)) + "%)</td>"
print "</tr></table>"
print "</td></tr>"
line = file.readline()
print "</table>"
print "<h2>Languages</h2>"
while line != "Totals grouped by language (dominant language first):\n":
line = file.readline()
print "<table>"
print "<tr><th>Language</th><th>Lines</th></tr>"
line = file.readline()
while line != "\n":
lang, lines, per = split ( line )
lang = lang[:-1]
print "<tr><td bgcolor=\"" + colors[lang] + "\">" + lang + "</td><td>" + lines + " " + per + "</td></tr>"
line = file.readline()
print "</table>"
print "<h2>Totals</h2>"
while line == "\n":
line = file.readline()
print "<table>"
print "<tr><td>Total Physical Lines of Code (SLOC):</td><td>" + strip(split(line,"=")[1]) + "</td></tr>"
line = file.readline()
print "<tr><td>Estimated development effort:</td><td>" + strip(split(line,"=")[1]) + " person-years (person-months)</td></tr>"
line = file.readline()
line = file.readline()
print "<tr><td>Schele estimate:</td><td>" + strip(split(line,"=")[1]) + " years (months)</td></tr>"
line = file.readline()
line = file.readline()
print "<tr><td>Total estimated cost to develop:</td><td>" + strip(split(line,"=")[1]) + "</td></tr>"
print "</table>"
file.close()
print "Please credit this data as \"generated using 'SLOCCount' by David A. Wheeler.\"\n"
print "</body>"
print "</html>"
『肆』 linux命令語法結構
以-開頭的:開關(switch),可以多個開關選項並在一起,比如tar -xjvf test.tar.bz2;
[ ]:可選的參數,就是說如果你不需要那個參數對應的功能就可以不用那個參數;
< > :被替換的部分,通常是必須有的,比如tar -xjvf <你要解壓縮的bz2檔案的文件名>
|:「或者」之意。注意不要和shell符號||和|搞混了啊;
{ }:……樓主此例中好像沒用花括弧啊-_-||……
/:就是說你如果使用了prefixlex要在前面加上/符號。
希望對你有用!
PS:Linux中大多數命令都是服從上述規范編寫的。但是注意一些古老的工具,如ps、tar,如下的命令調用也是合法的:
tar xjvf test.tar.bz2
ps ef
『伍』 用lex在linux下,C編譯提示找不到lex.h咋回事
我查了一下, 感覺是不是你在VC++里一些設置步驟還沒做? 在安裝了Parser Generator後,執行以下步驟,即可使VC++編譯和連接由Parser Generator產生的文件。 1.目錄設置 在VC++中執行以下步驟,每個步驟只執行一次。