做胃镜前喝的那个液体是什么| 头疼是什么原因引起| 高大的动物是什么生肖| 芹菜炒什么配菜好吃| 做梦梦见剪头发是什么意思| 什么是碧玺| 安睡裤是什么| 什么是劣药| 血糖高吃什么水果好| 剪头发叫什么手术| 贴切是什么意思| yy飞机票是什么| 本加一笔是什么字| 舌苔少是什么原因| 中药什么时候喝效果最好| 什么的舞蹈| 气郁症是什么症状| 明太鱼是什么鱼| 荨麻疹可以吃什么| 1989年出生是什么命| 病毒性结膜炎用什么眼药水| 荷叶泡水喝有什么作用| 梦见自己爷爷死了是什么预兆| 潇字五行属什么| 反流性食管炎可以吃什么水果| 什么茶对胃好| fvc是什么意思| 拔罐有什么用| 牡丹花像什么| 腹泻吃什么药最好| 麒麟长什么样| 夏天吃什么汤| 吃南瓜有什么好处| 盆腔炎是什么| 智是什么意思| 被螨虫咬了非常痒用什么药膏好| 阑尾炎输液输什么药| 面瘫什么意思| 绝经一般在什么年龄| 减肥吃什么油| 务实什么意思| 男人有腰窝意味着什么| 天蝎女跟什么星座最配| 虚热吃什么药| 灵芝主要治什么病| 泡黄芪水喝有什么好处| 过敏用什么药膏| 口头禅什么意思| 过期的钙片有什么用途| 来大姨妈适合吃什么水果| 阳历7月份是什么星座| 肺气肿用什么药效果好| 女性真菌感染是什么原因造成的| 腱鞘炎用什么药治疗| 2033年是什么年| 为什么打哈欠| rr医学上什么意思| 沉珂是什么意思| 吃什么食物可以补充雌激素| ch是什么| 头晕是什么症状| 不安腿综合征吃什么药| 忌入宅是什么意思| 领导谈话自己该说什么| 沙棘不能和什么一起吃| 高危型hpv52阳性是什么意思| 任性的女孩有什么特点| 正佳广场有什么好玩的| 上钟什么意思| 胸腔疼痛是什么原因| 攒肚是什么意思| 孟子姓什么| 里字五行属什么| 什么食物对心脏好| sod是什么| 什么样的云朵| 晚上喝红酒配什么小吃| 视网膜病变有什么症状| 胃字出头念什么| 达瓦里氏什么意思| 做梦梦到蛇是什么征兆| 条形码的数字代表什么| 桃子不能和什么水果一起吃| 思的五行属性是什么| 楞严神咒是什么意思| 泡脚什么时候泡最好| 血压高压低是什么原因| 亦木读什么| 经常吐是什么原因| 常放屁是什么原因| robam是什么牌子| 日主是什么意思| 手指疣初期什么样子| 糖尿病能吃什么| 米其林是什么意思| 脚发麻什么原因| 皮肤软组织感染是什么意思| 子宫内膜单纯性增生是什么意思| 白内障什么原因造成的| 牛肉用什么调料| 草龟吃什么| 心电图能检查出什么| 翻白草治什么病| 牙龈出血什么原因| 橘子什么季节成熟| 血府逐瘀片主治什么病| 按摩椅什么品牌最好| 大什么大| 形体是什么意思| 梦见买衣服是什么预兆| 阿斗是什么意思| 婴儿补铁吃什么铁剂| 毛豆有什么营养价值| 尿道感染是什么原因引起| 喝最烈的酒下一句是什么| 蜜蜡属于什么五行属性| 益生菌什么时候吃最好| 雨污分流什么意思| 咳嗽一直不好什么原因| 胃不好吃什么好| 咽炎吃什么药最管用| 尤文氏肉瘤是什么病| 血小板升高是什么原因| 晚上七点是什么时辰| 隐翅虫咬人后用什么药| 芹菜和西芹有什么区别| 手掌发红是什么原因| 吃生姜对身体有什么好处| itp是什么| 人的运气跟什么有关| 公道自在人心是什么意思| 缩量横盘意味着什么| 人棍是什么意思| 59岁属什么生肖| 完美收官什么意思| 没有料酒用什么去腥| 尿频吃什么药好| 万能受血者是什么血型| 缺钠是什么原因造成的| 什么主筋骨| bella什么意思| 变性淀粉是什么| 头晕头疼挂什么科| 支气管发炎是什么原因引起的| 什么叫肾功能不全| 七月初七是什么生肖| 孙俪是什么星座| 婴儿拉肚子吃什么药| 运动后体重增加是什么原因| 怕金森是什么症状| 狗是什么属性| 腋下长痘痘是什么原因| 6月26日是什么日子| 家里进蝙蝠什么预兆| 阑尾炎手术后吃什么| 核磁共振挂什么科| 炖鸡放什么调料好吃| 吃枸杞对身体有什么好处| 胆囊结石会引起身体什么症状| 风湿病是什么引起的| 坐镇是什么意思| inr医学上是什么意思| 芳心是什么意思| 花干是什么做的| 医院的特需门诊是什么意思| 滑膜炎吃什么药| 打呼噜挂什么科室| 腋下异味看什么科| 11月24日是什么星座| 挚友是指什么的朋友| 鱼油对身体有什么好处| 夜盲症是什么意思| 金玉良缘是什么生肖| 贷款是什么意思| 菁是什么意思| 人乳头瘤病毒33型阳性是什么意思| 气血不足有什么症状| 安宫牛黄丸有什么作用| 唔什么意思| 蜗牛吃什么| 去香港自由行要办什么手续| 红参对子宫有什么作用| 身上长疣是什么原因| 喝什么泡水降血压最好| opd是什么意思| 脖子粗大是什么病的症状| 胃火重口臭吃什么药好| 突然头晕是什么原因| 去医院检查艾滋病挂什么科| 土土心念什么| 口我什么意思| 585是什么金| 一什么二什么| 什么叫智慧| dq是什么意思| 眩晕症吃什么好| 28周检查什么项目| tax是什么意思| 领养孩子需要什么条件| 乳腺结节有什么危害| 学历证是什么| 嗓子有粘痰什么原因| 舌头有裂纹是什么原因| 过度纵欲的后果是什么| 12.24是什么星座| 跖围是什么意思| 夜尿频多是什么原因| 临盆是什么意思| 什么叫职业年金| 吃什么药去体内湿气| 紧锣密鼓是什么意思| dtc是什么意思| 十二月十号是什么星座| 什么是功德| 通草和什么炖最催奶了| 狗咬人后狗为什么会死| 第一次怀孕有什么反应| 大姨妈推迟是什么原因| 剖腹产坐月子吃什么| 舌强语謇是什么意思| 白酒都有什么香型| 火疖子是什么| 什么原因导致子宫内膜息肉| 凶猛的动物是什么生肖| 什么是桥本病| 肾阳虚有什么症状男性| 吃激素有什么副作用| 我的部首是什么| copd是什么病| 长脸适合什么发型| 维生素ad和维生素d有什么区别| 海螺吃什么食物| 拔气罐有什么好处| 168红包代表什么意思| 突然全身抽搐是什么病| 外耳炎用什么药| 广东菜心是什么菜| 壮的偏旁叫什么名字| 孕妇腰疼是什么原因| 老油条什么意思| 汞中毒有什么症状| 草酸对人体有什么危害| 晴纶是什么材质| 女人吃什么对卵巢和子宫好| 额头上有痣代表什么| 海水倒灌是什么意思| 白细胞十一是什么意思| saucony是什么品牌| 冲代表什么生肖| 嬴政和芈月是什么关系| 经常喝蜂蜜水有什么好处和坏处| 性早熟有什么症状| 什么是远视| 什么都不是| 酒精过敏吃什么药| 什么是白噪音| 碱性磷酸酶高吃什么药| 肺不好吃什么| 存在是什么| 脚经常抽筋是什么原因| 口关读什么| 丙肝病毒抗体阴性是什么意思| 2016年属什么| 泌乳素过高女性会出现什么症状| 百度
Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

On Parsing Perl

by merlyn (Sage)
on Dec 04, 2000 at 04:21 UTC ( [id://44722]=perlmeditation: print w/replies, xml ) Need Help??

{from an alt.perl post I just made, reposted here to solicit feedback from fellow monks...}

>>>>> "Makhno" == Makhno <mak@imakhno.freeserve.co.uk> writes: Makhno> I'm thinking of writing a GUI Perl-syntax-aware editor, and Makhno> wondering what's the best way to parse perl? Highlighting Makhno> reserved words is easy (using, eg, index()) but indentifying Makhno> things like comments is a bit more difficult. Makhno> A regex like /#.*\n/ will catch comments when they are used Makhno> simply, ie: Makhno> print "hello\n"; #print hello Makhno> but will get it wrong when the '#' is used as part of a regex Makhno> (or in a string) Makhno> s#hello#goodbye#; Makhno> print "will behave like a #comment"; Makhno> Does anybody have any ideas on how I go about parsing perl Makhno> syntax in such a way, before I go to a lot of potentially Makhno> unnecessary work?
Perl is extremely difficult to parse. In fact, some would say impossible.

One thing that makes it difficult is the dual nature of a half dozen characters like "/". If that / is being used in a place that's expecting an operator, it's divide. If it's being used in a place that's expecting an operand, it's the beginning of a regular expression. So you have to keep track at all times of whether you're looking for an operator or an operand.

"No problem", you say? Quick... for the following, play the game of "regex or divide?"

sin / ... time / ... localtime / ... caller / ... eof / ...
Got those right? How about these?
use constant FOO => 35; FOO / ... use Fcntl qw(LOCK_SH); LOCK_SH / ...
OK, and now some of your own:
sub no_args (); sub one_arg ($); sub normal (@); no_args / ... one_arg / ... normal / ...
Got those too? How about these (same problem, different file):
use Random::Module qw(aaa bbb ccc); aaa / ... bbb / ... ccc / ...
A little harder, eh? So now you have to parse OUTSIDE the file to get your answer. And as if that wasn't enough, let's get weird:
BEGIN { eval (time % 2 ? 'sub zany ();' : 'sub zany (@);'); } zany / ...
Quick, was that last one a divide or a regex start?

Why does it matter? Look at this:

sin / 25 ; # / ; die "this dies!"; time / 25 ; # / ; die "this doesn't die";
The first one is computing the sin of the true/false value gotten by matching " 25 ; # " against $_. Then it dies. The second one is computing the time of day divided by 25, then ignoring the comment.

Starting to see the trouble?

This leads people to say "the only thing which can parse Perl (the language) is perl (the binary)". Maybe not for Perl6. But for the Perl we know and can use today, certainly so.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: On Parsing Perl
by quidity (Pilgrim) on Dec 04, 2000 at 04:40 UTC

    I do most of my perl coding using CPerl mode for Xemacs, and although it is very good at spotting syntax it is often horribly wrong, especially when odd quoting characters or pod is brought into the equation. eval is even worse. I'd advise anyone even thinking of trying to parse perl to look at what can be achieved, and then either improving that (to the benefit of everyone) or just to give up.

    I do sometimes find myself chosing a particular way of coding over another (possibly better) way because the second breaks the pretty printing, and I want others using the same editor to be able to read the code I write.

      I hate to say this, but even as an XEmacs fan, cperl+gemacs is far superior. Recent GNU Emacs has some extra stuff that XEmacs doesn't have, that allows cperl to do some really amazing things. (I have vague memories of the "extra stuff" being multiple syntax transition tables for each character, so you can gracefully handle things like m!!x and other non-standard delimiters. But I could be totally wrong.)
Re: On Parsing Perl
by repson (Chaplain) on Dec 04, 2000 at 07:28 UTC
    You could use B::Deparse for some of it, which eliminates some of what you don't want, but even that fails on many other things. The best bet is to code for the majority of perl and leave programmers to use their heads for the rest. This is what I do with syntax highlighting in vim, I use it generally but don't belive it for a moment. It is still sometimes helpful anyway. This is the way it will have to stay for now, at least until Perl6...
Re: On Parsing Perl
by toadi (Chaplain) on Dec 04, 2000 at 13:49 UTC
    I'm with you merlyn. I use vim(*nix) and textpad(windows), both make some mistakes. Like in some regex syntax it does some weird things...


    --
    My opinions may have changed,
    but not the fact that I am right

Re: On Parsing Perl
by nop (Hermit) on Dec 04, 2000 at 21:21 UTC
    I use the perl mode on emacs, and resort to small tricks to keep everything ok. For example,
    s/'"/;
    upsets the syntax colorization badly (as emacs thinks following code is in the string), so I use idioms like
    s/'"/; #"'
    to "close" my "open" strings....
Eight years later...
by samwyse (Scribe) on Jan 13, 2009 at 19:08 UTC
    I decided to run this test script under various versions of Perl.
    @examples = split /\n/, <<'EXAMPLES'; sin / ... time / ... localtime / ... caller / ... eof / ... use constant FOO => 35; FOO / ... use Fcntl qw(LOCK_SH); LOCK_SH / ... sub no_args (); sub no_args{1}; no_args / ... sub one_arg ($); sub one_arg{1}; one_arg / ... sub normal (@); sub normal{1}; normal / ... EXAMPLES for (@examples) { s=\.\.\.=25 ; # / ; die "this dies!";=; local($a) = eval; $a = $@ if $@; print "$_\n\t$a\n"; }
    I don't know what the results would be for earlier versions, but from Perl 5.6 onwards it's pretty consistent.
    Example5.0065.0085.010
    sindiesdiesdies
    time49274891.7249274891.7449274891.76
    localtimediesdiesdies
    callerdiesdiesdies
    eofdiesdiesdies
    FOO1.41.41.4
    LOCK_SHdiesdies0.04
    no_argsdiesdiesdies
    one_argdiesdiesdies
    normaldiesdiesdies
    Most of the "dies" instances also produced this message: Warning: Use of "XXX" without parentheses is ambiguous at (eval N) line 1. However, the LOCK_SH example never generated errors, while the last three generated "Prototype mismatch" messages. I must also note that almost all of the examples generated warnings, despite using neither the '-w' option or 'use strict;'
Re: On Parsing Perl (Once upon a time)
by Anonymous Monk on Jul 11, 2022 at 11:27 UTC
    I'm currently working on something, (basically perl parser), and apart from the BEGIN block, everything seems parseable (is that the right word?) using some simple LL grammars. Or am I just too uneducated?
      everything seems parseable (is that the right word?) using some simple LL grammars.

      No, only perl (the interpreter) can parse all of Perl (the language). See my node here for details.

      Edit: added emphasis.

      Static parsing is only reliable, if you rule out or control all imported subs, because prototypes change the way Perl is parsed. See HaukeX's other reply.

      Basically changes at compile time ( see BEGIN blocks ) can change the parser.

      Dynamic parsing is possible though, if you inspect the op-tree after compilation, that's the basic idea of some newer tools, like the perlnavigator.

      See also perl -c in perlrun or B::Xref

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Static parsing is only reliable, if you rule out or control all imported subs, because prototypes change the way Perl is parsed.

        I think, though, that prototypes aren't the only reason Perl isn't statically parseable. There are quite a few heuristics that the parser uses that aren't all too well documented, and I'm not sure if a static parser would be able to reimplement all of them. And then there is no strict code, which I think gets even trickier. At some point I was considering researching and making a list of all of the reasons, but I unfortunately never got around to it.

Re: On Parsing Perl
by gaggio (Friar) on Dec 04, 2000 at 04:35 UTC
    I don't know if I am with you there. What is the Perl executable doing when it executes a script? Isn't that called parsing also?
      Stunning reading comprehension there. I wonder if such clueless folk ever come back and read their comments years later and feel a twinge of embarrassment.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://44722]
Approved by root
Front-paged by htoug
help
Chatterbox?
and a moth chases the moon...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2025-08-06 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.


    盲从什么意思 6月29什么星座 梅花象征着什么 黑枸杞泡茶是什么颜色 医疗美容需要什么资质
    新疆有什么特产 为什么吃饱了就犯困 胜利在什么 扒灰是什么意思 什么中毒查不出来
    肺部斑片状高密度影是什么意思 满载而归的载是什么意思 前列腺液是什么东西 什么床不能睡觉 剁椒是什么辣椒
    有氧运动和无氧运动有什么区别 心慌吃什么药能缓解 精索静脉曲张是什么 八哥鸟吃什么 手心痒是什么原因
    八面玲珑指什么生肖hcv7jop6ns4r.cn 左肾积水有什么症状hcv9jop7ns9r.cn 上海有什么景点hcv9jop8ns3r.cn 琼字代表什么生肖hcv8jop5ns1r.cn 为什么英文怎么写hcv8jop1ns9r.cn
    女的肾虚是什么原因引起的hcv9jop7ns0r.cn 破伤风是什么意思hcv9jop0ns5r.cn 走路脚后跟疼是什么原因wmyky.com 什么是戒断反应hcv9jop4ns6r.cn 神经性皮炎不能吃什么食物hcv8jop7ns7r.cn
    做梦拉粑粑是什么意思hcv9jop3ns5r.cn 拔罐拔出水是什么原因hcv7jop5ns0r.cn 早上起来腰疼是什么原因hcv8jop3ns7r.cn 农历六月十五是什么星座hcv7jop9ns9r.cn 方圆什么意思hcv8jop6ns1r.cn
    吃什么精力旺盛有精神520myf.com 精益求精的意思是什么hcv8jop2ns1r.cn pes是什么材质hcv9jop3ns2r.cn 囊性无回声是什么意思hcv9jop2ns8r.cn 藕是莲的什么部位xinmaowt.com
    百度